I want to create a motion gesture dataset and I'm trying to isolate skin color using bgr2hsv. So far, my output window displays the exact 'black and white video' feed that i aim to achieve. But the video file that i store isn't the same. Do help me fix this.
I'm new to this, i tried doing what i could. If there's a silly mistake, do guide me.
import numpy as np
import os
import cv2
filename = 'video.mp4'
frames_per_second = 10.0
res = '720p'
def change_res(cap, width, height):
cap.set(3, width)
cap.set(4, height)
STD_DIMENSIONS = {
"480p": (640, 480),
"720p": (1280, 720),
"1080p": (1920, 1080),
"4k": (3840, 2160),
}
def get_dims(cap, res='1080p'):
width, height = STD_DIMENSIONS["720p"]
if res in STD_DIMENSIONS:
width,height = STD_DIMENSIONS[res]
change_res(cap, width, height)
return width, height
VIDEO_TYPE = {
'avi': cv2.VideoWriter_fourcc(*'XVID'),
'mp4': cv2.VideoWriter_fourcc(*'XVID'),
}
def get_video_type(filename):
filename, ext = os.path.splitext(filename)
if ext in VIDEO_TYPE:
return VIDEO_TYPE[ext]
return VIDEO_TYPE['mp4']
cap = cv2.VideoCapture(1)
out = cv2.VideoWriter(filename, get_video_type(filename), 25, get_dims(cap, res))
while True:
ret, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
lower_blue = np.array([0, 48, 80])
upper_blue = np.array([20, 255, 255])
mask = cv2.inRange(hsv, lower_blue, upper_blue)
result = cv2.bitwise_and(frame, frame, mask = mask)
out.write(result)
cv2.imshow('frame', mask)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
out.release()
cv2.destroyAllWindows()