0

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()
  • I found that imwrite has trouble with images being 0-1 float, while imshow does not, I suppose VideoWriter could be similar. I suggest you check whether the image you are trying to write is 8 bit unsigned or floating point data. If the latter, try to scale to 0-255 and save as 8bit unsigned – Demosthenes Aug 24 '19 at 14:19
  • If you'd guide me how to (possibly with code) then it would be a favor. – Rishabh Patel Aug 24 '19 at 14:59
  • Come on, you‘re in python, just print the image and see if it‘s float in the range 0-1 or into between 0 and 255... – Demosthenes Aug 24 '19 at 15:05

1 Answers1

0

I found the Solution!

frame = cv2.cvtColor(mask, cv2.COLOR_GRAY2RGB)

source: How to save masks of videos in openCV2 python