WEB camera capture Ver3

The one created last time. Webcam capture Ver2

The last time was a program that used GPIO.

This time, a shooting program that specifies the number of shots.

The basic configuration is the same as last time.

Library

cap_save_prg.py


import cv2
import os,os.path
from pathlib import Path
import sys
from datetime import datetime
import time

I added pathlib to the basic configuration.

I'm investigating various things around here, so it's quite appropriate.

Definition of save destination and file name

cap_save_prg.py


save_dir_path = 'Destination'
filename = 'file name'

It doesn't matter where you like. Recently, I created a folder on the desktop and saved the program in it. I try to save the captured image.

Creating a directory

cap_save_prg.py


os.makedirs(save_dir_path,exist_ok=True)
base_path = os.path.join(save_dir_path,filename)
datename = datetime.now().strftime('%m%d%H%M')

It's as usual.

Definition of device used

cap_save_prg.py


device_id = 0
width = 640
height = 480
fps = 30

cap = cv2.VideoCapture(device_id)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
cap.set(cv2.CAP_PROP_FPS, fps)

This time I found a description of the downlink that can set the function of Opencv while investigating I decided to try it out, and the description is very popular.

Well, if you just shoot, there is no problem with cv2.VideoCapture ().

Imaging

cap_save_prg.py


n = 0
while True:
    ret,frame = cap.read()
    cv2.imshow(filename,frame)
    key = cv2.waitKey(1) & 0xFF 

    cv2.imwrite((base_path + datename +'_'+ str(n) + ".png "),frame)
    pathl,dirsl,filesl = next(os.walk(save_dir_path))
    file_count = len(filesl)
    print(file_count)
    time.sleep(0.2)
    cap.release
    n += 1

    if file_count == 100:
        break

This time, shooting started at the same time as the program started I am trying to count the number of files in the save destination directory.

The program will stop when the set value is reached.

If true, put ʻif key == ord ('s'):beforecv2.imwriteand press the keyboard I wanted to start shooting NameError: name'file_count' is not defined` occurs.

I did a lot of research, but I couldn't solve it, so I decided to go with it.

Summary

I try to do this even with one program, so my brain can't handle it, and wisdom fever is generated ... I think I have no choice but to improve the things I made one by one. Well, I'm doing it while uploading shobo things and writhing.

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