live frames from webcam - python

import numpy as np
import cv2
#holder for the camera object
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here (color)
color = cv2.cvtColor(frame, 1)
# Display the frame
cv2.imshow('LiveCam',color)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
'''When everything done, release the capture'''
cap.release()
cv2.destroyAllWindows()
This code will display my own live frames on my own screen.
I want to send my live frames to another computer,and to receive the other's computer own live in order to display it.
Can I get some help please?

Related

my camera doesnt turn on while Coding in opencv

I am beginner in CV. My camera crash every time.
I mean camera light is ON but the there is no camera display
Could someone help me?
Here is the code
import cv2 as cv
import numpy as np
cap=cv.VideoCapture(0)
# read webcam untill the end
while (cap.isOpened()):
# capture frame by frame
ret,frame=cap.read()
if ret == True:
# to display frame
cv.imshow("Frame",frame)
else:
break
cv.waitKey(1)
cap.release()
cv.destroyAllWindows()
i think you put waitKey in wrong place, I hope below code will work !
import cv2 as cv
cap = cv.VideoCapture(0)
# read webcam untill the end
while (cap.isOpened()):
# capture frame by frame
ret, frame = cap.read()
if ret == True:
# to display frame
cv.imshow("Frame", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv.destroyAllWindows()
#tahir mehmood. I modified your code, and I tested it. Worked well.
import cv2 as cv
import numpy as np
cap=cv.VideoCapture(0)
# read webcam untill the end
while (cap.isOpened()):
# capture frame by frame
ret,frame=cap.read()
if not ret:
break
cv.imshow("Frame",frame)
cv.waitKey(1)
cap.release()
cv.destroyAllWindows()

Python VideoCapture(0) my camera bad work

This my codes :
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
I write the code like this and try to open my camera, but the camera image looks like the screenshot.
Stop working screen shot
Any idea ? What is the reason?
It's indentation problem.
Try this code:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
Provide proper indentation so the program works fine.
Press "q" to stop the program.

Why does OpenCv returns a false ret, frame=cap.read()?

I'm simply trying to get the image of my webcam in python with OpenCV 4.2.0 (on Spyder - python 3.7 running on windows 10). I just copy-pasted the code of the OpenCV documentation (see below) and most of the time it prints: "Can't receive frame (stream end?). Exiting ..."
So I know the error comes from: ret, frame = cap.read(). But I don't know how to fix this.
Sometimes it prints: "Cannot open camera" and then it exits the Spyder Console and starts a new one
And sometimes it just works great...
Can anyone help?
Here is my code:
import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)
if not cap.isOpened():
print("Cannot open camera")
exit()
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# if frame is read correctly ret is True
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
break
# Our operations on the frame come here
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
# Display the resulting frame
cv.imshow('frame', gray)
if cv.waitKey(1) == ord('q'):
break
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()
Added the last line to close the window
I had a similar issue (now for a while). I run Linux, not Windows. I read video feeds from multiple IP cameras, not just one webcam. But I believe the solution would be the same. In my opinion, it looks as if the feed, or OpenCv stops supplying frames. So I simply recreate cap, test ret, and move on. This will create a endless loop, it will never stop. In my scenario this correct. I want to real all frames, all the time.
It will look something like:
import numpy as np
import cv2 as cv
cap = cv.VideoCapture('rtsp://1.2.3.4:554//Streaming/Channels/1')
if not cap.isOpened():
print("Cannot open camera")
exit()
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# if frame is read correctly ret is True
while ret == False:
print("Can't receive frame. Retrying ...")
cap.release()
cap = cv.VideoCapture('rtsp://1.2.3.4:554//Streaming/Channels/1')
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
print('frame')
# Display the resulting frame
cv.imshow('frame', gray)
if cv.waitKey(1) == ord('q'):
break
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()
import numpy as np
import cv2 as cv
cap = cv.VideoCapture(0)# In place of zero we gonna use path of the video file.
while cap.isOpened():
# Capture frame-by-frame
ret, frame = cap.read()
# if frame is read correctly ret is True
if ret:
# Our operations on the frame come here
gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
# Display the resulting frame
cv.imshow('frame', gray)
if cv.waitKey(1) == ord('q'):
break
else:
print("camer not streaming")
# When everything done, release the capture
cap.release()
cv.destroyAllWindows()

Resize frame of cv2.VideoCapture

I'm trying to resize frame image-array in original (480,640,3) to (224,224,3), but I use
CV_CAP_PROP_FRAME_WIDTH, CV_CAP_PROP_FRAME_HEIGHT, only change size of frame that displayed on my screen. Thank you very much!
My code here!
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
frame = cv2.flip(frame, 1)
# Display the resulting frame
cv2.imshow('frame',frame)
print(frame.shape)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
you can add following code in your while loop,
frame = cv2.resize(frame, (224, 224))
print(frame.shape)

error with cv2.destroyAllWindws() on ubuntu 14.04

I'm programing in python and opencv on Ubuntu but the cv2.destoyAllWindows() doesn't seem to work,as in this example:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display the resulting frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
when i press 'q' the program end but the window remain on the screen how ever on windows i didn't face such thing
any help I will be thankful

Categories

Resources