Skip to content

Instantly share code, notes, and snippets.

@rruntsch
Created January 20, 2022 17:16
Show Gist options
  • Save rruntsch/ba2c41e3cc8ad8287b3b651f411f4746 to your computer and use it in GitHub Desktop.
Save rruntsch/ba2c41e3cc8ad8287b3b651f411f4746 to your computer and use it in GitHub Desktop.
The Python class c_video_frame_capture.py captures video frames as still image files in JPEG format
#
# Name: c_video_frame_capture.py
# Date: January 19, 2022
# Author: Randy Runtsch
#
# Description:
#
# The c_video_frame_capture class displays video from a webcam.
# When the user presses "p" it writes the a frame to a file
# as a JPEG image.
#
# This classes uses the opencv library to display video and write frames.
# opencv tutorial: https://docs.opencv.org/4.x/d6/d00/tutorial_py_root.html
# Import the opencv (cv2) and datetime libraries.
import cv2
from datetime import datetime
class c_video_frame_capture:
def __init__(self, camera_id, img_file_nm_base):
# This constructor is designed to handle single images with the press of the 'p' key.
# Base of image file location, including directory (for example, on Windows use forward slash: c:/frame_capture).
self.img_file_nm_base = img_file_nm_base
# Define the video capture object.
self.vid_cap = cv2.VideoCapture(camera_id)
if not (self.vid_cap.isOpened()):
print("Could not open camera.")
return
self.capture_video()
def capture_video(self):
# Loop infinitely to view video.
# When "p" is pressed, save a picture to a file.
# When "q" is pressed, quit the program.
while(True):
# Capture the video, frame by frame
ret, frame = self.vid_cap.read()
# Display the resulting frame
cv2.imshow('Frame', frame)
# Handle key presses.
key = cv2.waitKey(1)
if key == 112:
# Save frame when 'p' (ASCII code 112) is pressed.
self.write_frame(frame)
elif key == 113:
# Quit programe when 'q' (ASCII code 113) is pressed.
return
def write_frame(self, frame):
# Write the video frame to a JPEG file.
now_string = self.get_now_string()
cv2.imwrite("%s_%s.jpg" % (self.img_file_nm_base, now_string), frame)
def get_now_string(self):
# Return the current date time in this format: "YYYYMMDD_HHMMSS_microsecond".
now = datetime.now()
now_string = now.strftime("%Y") + now.strftime("%m") + now.strftime("%d") + '_' + now.strftime("%H") + now.strftime("%M") + now.strftime("%S") + '_' + now.strftime("%f")
return now_string
#
# Name: get_cameras.py
# Date: January 19, 2022
# Author: Randy Runtsch
#
# Description:
#
# Use the pygrabber package to obtain
# the cameras active on the Windows computer.
# Note that this technique works only on Windows.
from pygrabber.dshow_graph import FilterGraph
cameras = FilterGraph().get_input_devices()
print(cameras)
#
# Name: run_video_frame_capture.py
# Date: January 19, 2022
# Author: Randy Runtsch
#
# Description:
#
# Use the c_vido_frame_capture class to write
# webcam video frames to jpeg files in the
# specified folder.
from c_video_frame_capture import c_video_frame_capture
# Instantiate the c_video_frame_capture class with the camera ID and the name of the folder
# where frame capture JPEG files are to be stored. Note that the default camera ID is 0. To
# use other cameras, you will need to experiment. On Windows, run get_cameras.py
# obtain the list of cameras on the computer.
c_video_frame_capture(1, 'c:/frame_capture/frame')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment