0

I can't see any output video window when I run the code. Python code:

import cv2
import numpy as np

#cap = cv2.VideoCapture(0)
cap = cv2.VideoCapture('C:\Users\khan1\Desktop\pythonproject\test_1.avi')
print cap
while (cap.isOpened()):
    _, frame = cap.read()
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    lower_green = np.array([65, 60, 60])
    upper_green = np.array([80, 255, 255])
    mask = cv2.inRange(hsv, lower_green, upper_green)
    res = cv2.bitwise_and(frame, frame, mask=mask)

    cv2.imshow('frame', frame)
    cv2.imshow('mask', mask)
    cv2.imshow('res', res)

    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break

cv2.destroyAllWindows()
cap.release()

When I run it I get Process finished with exit code 0 but it doesn't show any video. If I use cap = cv2.VideoCapture(0) for webcam, this code works fine showing 3 different video window. Also print cap shows <VideoCapture 034523B0>. What am I doing wrong?

sayem48
  • 101
  • 5
  • 14

2 Answers2

0

Let's go over the code provided because this output is what should be expected.

This line is activating your main webcam. If you only have one webcam plugged into your machine then this should be fine.

cap = cv2.VideoCapture(0)

Next the loop is simply going to capture your webcam's frames until you press the Escape key. You are getting three windows because you are running the imshow function three times:

cv2.imshow('frame', frame)
cv2.imshow('mask', mask)
cv2.imshow('res', res)

The frame variable stores the normal frames via the webcam without modification. The mask variable is running a color threshold based on the range of values provided, which in this case is a range between lower_green and upper_green. The res variable is going to display the mask on top of the normal frames hence the bitwise_and operator.

Note that the default color format for opencv is BGR(Blue, Green, Red) and you are converting it to an HSV value, which is why you are probably getting two black screens.

Change the value in your color threshold until you get a better result or try this code which will mask anything not remotely green.

import cv2
import numpy as np

cap = cv2.VideoCapture(0)
#cap = cv2.VideoCapture('C:\Users\khan1\Desktop\pythonproject\test_1.avi')
print cap
while (cap.isOpened()):
    _, frame = cap.read()
    rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
    lower_green = np.array([0, 60, 0])
    upper_green = np.array([200, 255, 200])
    mask = cv2.inRange(rgb, lower_green, upper_green)
    res = cv2.bitwise_and(frame, frame, mask=mask)

    cv2.imshow('frame', frame)
    cv2.imshow('mask', mask)
    cv2.imshow('res', res)

    k = cv2.waitKey(1) & 0xFF
    if k == 27:
        break

cv2.destroyAllWindows()
cap.release()

You'll notice that in the code provided I am converting it to RGB format not HSV.

avereux
  • 572
  • 1
  • 5
  • 15
  • Thanks for explaining this code for me. But my question was why can't I read the video file (test_1.avi) stored on my laptop? Actually I am not seeing any screen. It works fine with webcam, but can't read the video file from the mentioned path. Also BGR2RGB suggestion was also helpful. – sayem48 Jun 19 '17 at 03:07
  • How do I solve that? Is there any known link that I can follow to solve this? – sayem48 Jun 19 '17 at 15:44
  • https://stackoverflow.com/questions/31040746/cant-open-video-using-opencv – avereux Jun 19 '17 at 15:47
0

For my 32 bit windows 10 laptop, I copied opencv_ffmpeg320.dll from C:\OpenCV-3.2.0\opencv\build\bin to C:\Python27 and that solved the problem. OpenCV 2.4 VideoCapture not working on Windows is the link I followed.

sayem48
  • 101
  • 5
  • 14