[PYTHON] Web camera capture Ver 2

When shooting with Raspberry Pi I think that the first place to go is to shoot by keyboard operation. However, when it comes to taking a large number of images It is impossible for ordinary people because it is necessary to fire a high number of people. So I created it thinking that it would be easier to shoot automatically.

This time I made a subordinate (Sacrifice No. 1) so I made it together

environment

Raspberry Pi 3 Model B+ os Raspbian(Bastar) Python 3.7.3 opencv 4.1.0.25 Camera used ELECOM UCAM-C750FBBK

Library

cap_save.py


import cv2
import os
import sys
import datetime  
import RPi.GPIO as GPIO
from time import sleep
import time

Since GPIO is used this time I added it to the basic shooting set I use a timer, so I add it.

basic configuration

cap_save.py


GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(25, GPIO.OUT)

This time I attached a tact switch to the breadboard Since it is used to start and end shooting, it is set so that it can be done.

Shooting program

cap_save.py


def save_frame_camera_cycle(device_num, dir_path, basename, cycle, ext='jpg', delay=1, window_name='frame'):
    cap = cv2.VideoCapture(0)

    if not cap.isOpened():
        return

    os.makedirs(dir_path, exist_ok=True)
    base_path = os.path.join(dir_path, basename)
    
  n = 0
    while True:
        ret, frame = cap.read()
        cv2.imshow(window_name, frame)
        
        if GPIO.input(24) == GPIO.HIGH:
            GPIO.output(25, GPIO.HIGH)
            
            while True:   
                
                ret,frame = cap.read()
                cv2.imshow(window_name, frame)
                key = cv2.waitKey(1) & 0xFF
                if n == cycle:
                    n = 0
                    GPIO.output(25, GPIO.HIGH)
                    cv2.imwrite('{}_{}.{}'.format(base_path, datetime.datetime.now().strftime('%F %T'), ext), frame)
                    print("shoot")
                n += 1
    
                if GPIO.input(22) == GPIO.HIGH:
                    GPIO.output(25, GPIO.LOW)
                    print ("stop")
                    time.sleep(1)
                    break                
        else:
            GPIO.output(25, GPIO.LOW)
            
        if GPIO.input(22) == GPIO.HIGH:
            GPIO.output(25, GPIO.LOW)
            print ("end")
            break
        
    cv2.destroyWindow(window_name)
    GPIO.cleanup()

save_frame_camera_cycle(0,'/home/pi/Desktop/GPIO/test/photo', 'test', 20) 

Press the tact switch (24) to shoot every second. Press the tact switch (22) to stop shooting. Press the tact switch (24) in the stopped state to resume shooting. Press the tact switch (22) in the stopped state to end shooting. LED (25) lights up during shooting.

I made it while researching various things, so I do not understand the details I'm OK because I was able to shoot.

Actual wiring

The wiring to the breadboard is like this, based on various materials.

2020-09-03 (1).png

Summary

I ran it for about 4 hours, but it worked without stopping. However, it did not stop automatically by specifying the number of sheets. Currently under construction.

I've been touring around the web in various ways, but I'm not sure why it doesn't work these days.

that's all.

Recommended Posts

Web camera capture Ver 2
WEB camera capture Ver3
WEB camera capture
Python, OpenCV camera capture
Camera capture with Python + OpenCV
Get web screen capture with python