displaying the camera feed in grayscale in python with opencv - python

i've been trying to display the camera feed from my laptops web cam in grayscale and i've done it using the following code:
import cv2
import numpy as np
clicked = False
def onMouse(event, x, y, flags, param):
global clicked
if event == cv2.cv.CV_EVENT_LBUTTONUP:
clicked = True
cv2.namedWindow('image capture', cv2.WINDOW_NORMAL)
cv2.setMouseCallback('image capture', onMouse)
#initialize the camera object with VideoCapture
camera = cv2.VideoCapture(0)
sucess, frame = camera.read()
cv2.imwrite('snapshot.png', frame)
gray = cv2.imread('snapshot.png', cv2.IMREAD_GRAYSCALE)
while sucess and cv2.waitKey(1) == -1 and not clicked:
cv2.imwrite('snapshot.png', frame)
gray = cv2.imread('snapshot.png', cv2.IMREAD_GRAYSCALE)
cv2.imshow('image capture', gray)
sucess, frame = camera.read()
cv2.imwrite('snapshot.png', frame)
print 'photo taken press any key to exit'
cv2.waitKey()
cv2.destroyAllWindows()
Here what i've done is saved the frame in 'snapshot.png' and again reloaded it in grayscale and display that grayscale image. Is there any method to directly read the camera frame in grayscale rather than going through all this mess. Thanks in advance.

wow, what a mess ;)
you simply want:
gray = cv2.cvtColor( img, cv2.COLOR_BGR2GRAY )

In the latest version of opencv, the cvtColor expects it's scr to be not None and therefore gives 215-assertion error.
This is basically like a scenario where you have to use a catch block and try to handle exceptions.
Code to overcome this problem:
while True:
ret, frame = cap.read()
if frame.any():
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', gray)

Related

Show another image while showing one image in loop in opencv python

I am trying to show an image, named "Result", And if a person clicks on the image then, it should show another image, named "Result image", but after clicking, the image Result which has to be live input from webcam freezes. Can anyone help me with this ?
Here is my code :
import cv2
cap = cv2.VideoCapture(0)
def showImage(event,x,y,flags,param):
if event == 1:
cv2.imshow('Result Image', img)
cv2.namedWindow('Result')
cv2.setMouseCallback('Result', showImage)
while True:
_, img = cap.read()
cv2.imshow('Result', img)
cv2.waitKey(1)
I have tried to set the waitKey(1) if the image is clicked in the if event == 1 statement
This code worked for me:
import cv2
def showImage(event,x,y,flags,param):
if event == 1:
cv2.imshow('Result Image', img)
cv2.namedWindow('Result')
cv2.setMouseCallback('Result', showImage)
cv2.namedWindow('Result Image')
cap = cv2.VideoCapture(0)
while True:
_, img = cap.read()
cv2.imshow('Result', img)
if cv2.waitKey(1) != -1: break
cv2.destroyAllWindows()

OpenCV2 Write to webcam python

Can I do something like this?
import cv2
cap = cv2.VideoCapture(0)
wri = cv2.device(0)
while 1:
ret, val = cv2.cvtColor(cap.read(), cv2.RGB2GRAY)
if ret:
wri.setCurrentFrame(val)
if cv2.WaitKey(0) == 27:
break
cap.release()
wri.release()
cv2.destroyAllWindows()
I want to make something like ManyCam.
I have researched and have not been able to find anything like this.
Note that the VideoCapture object is strictly responsible for capturing(read) an image from the stream(webcam, video file, RTSP, e.t.c.).
Then you can do any further processing with any of the opencv functions
Now, the webcam hardware is a device you read from, not write to.
Try of this way
import cv2
capture = cv2.VideoCapture(0)
while(True):
ret, frame = capture.read()
grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('video gray', grayFrame)
cv2.imshow('video original', frame)
if cv2.waitKey(1) == 27:
break
capture.release()
cv2.destroyAllWindows()
That's it

Webcam input sent to pyvirtualcam is blue(using pyvirtualcam and opencv)

I am trying to get input from my webcam using OpenCv and send it to a virtual camera using pyvirtualcam. For some reason when my webcam is displayed it gives it a blue filter. When i display my webcam without sending it to virtual camera there is no filter and everything works well.
import pyvirtualcam
import cv2
cap = cv2.VideoCapture(0)
with pyvirtualcam.Camera(width=1280, height=720, fps=20) as cam:
while True:
ret_val, frame = cap.read()
frame = cv2.resize(frame, (1280, 720), interpolation=cv2.BORDER_DEFAULT)
# cv2.imshow('my webcam', frame)
cam.send(frame)
cam.sleep_until_next_frame()
if cv2.waitKey(1) == 27:
break # esc to quit
cv2.destroyAllWindows()
OpenCV uses BGR as pixel format. pyvirtualcam expects RGB by default, but it supports BGR as well:
fmt = pyvirtualcam.PixelFormat.BGR
with pyvirtualcam.Camera(width=1280, height=720, fps=20, fmt=fmt) as cam:

failing to play the whole video using cv2

i am trying to play a video using cv2 but it's only showing one frame and the video disappears
import cv2
img_file = 'car image.jpg'
video = cv2.VideoCapture('Tesla Dashcam AccidentTrim.mp4')
while True:
(read_successful, frame) = video.read()
if read_successful:
grayscaled_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
else:
break
classifier_file = 'car_detector.xml'
#Display the image with the faces spotted
cv2.imshow('Newton Caffrey Face Detector', grayscaled_frame)
#Don't Autoclose here (wait here in the code and listen for a key press)
cv2.waitKey(1)
I used the following code for displaying the video using cv2. It will keep displaying frames untill video ends. hope this will work for you, peace!
import cv2
cap = cv2.VideoCapture("Video.mp4")
width = 400
height = 300
num = 0
while True:
ret, frame = cap.read()
if ret:
frame = cv2.resize (frame, (width, height))
cv2.imshow("frame", frame)
if cv2.waitKey(1) & 0xff == ord('q'):
break
cv2.destroyAllWindows()

how to save an image once certain condition accures in cam or video in opencv

hello i want to save an image if it meets certain condition in video capture
for example save an image if the person do the v sign for example how can i do that?
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while True:
tf, img = cap.read()
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
#cv2.imwrite('v_sign.png',img)
cv2.imshow('img', img)
if img == cv2.imread('v_sign.png'):
cv2.imwrite('captured frame.png',img)
key = cv2.waitKey(1)
if key == 27:
break
cap.release()
cv2.destroyAllWindows()

Categories