cv2.VideoCapture( ' html live stream'? ) - python

Gets error when trying to open livestream.
I'm streaming this from my raspberry pi to my view it on windows so I can run opencv and yolo.
Is there a way to do this with opencv?
import cv2
cap = cv2.VideoCapture('http://______/html/#')
while True:
ret, frame = cap.read()
cv2.imshow("frame", frame)
key = cv2.waitKey(50)
if key == 27:
break
cv2.destroyAllWindows()

So the URL I was trying to connect to had a little display of the livestream from the raspberry pi and the settings under it. What I had to do along with the code from this answer was right click on the live stream display, open it up in another tab and use the URL from that tab. It's about 2-3 seconds off though.
https://stackoverflow.com/a/57539561/17280268
import cv2
cap = cv2.VideoCapture('http://345.63.46.1256/html/cam_pic_new.php?time=1642941457007&p')
#^^ Opened in new tab URL ^^
#cap = cv2.VideoCapture('http://345.63.46.1256/html/')
cv2.namedWindow('live cam', cv2.WINDOW_NORMAL)
while(True):
ret, frame = cap.read()
#img_resize = cv2.resize(frame, (960, 540))
cv2.imshow('live cam', frame)
if cv2.waitKey(50) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()

Related

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

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()

Cannot Close Video Window in OpenCV

I would like to play a video in openCV using python and close that window at any time, but it is not working.
import numpy as np
import cv2
fileName='test.mp4' # change the file name if needed
cap = cv2.VideoCapture(fileName) # load the video
while(cap.isOpened()):
# play the video by reading frame by frame
ret, frame = cap.read()
if ret==True:
# optional: do some image processing here
cv2.imshow('frame',frame) # show the video
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cv2.waitKey(1)
cv2.destroyAllWindows()
cv2.waitKey(1)
The window opens and starts playing the video, but I cannot close the window.
You can use cv2.getWindowProperty('window-name', index) to detect if the window is closed. I'm not totally sure about the index but this worked for me:
import cv2
filename = 'test.mp4'
cam = cv2.VideoCapture(filename)
while True:
ret, frame = cam.read()
if not ret:
break
cv2.imshow('asd', frame)
cv2.waitKey(1)
if cv2.getWindowProperty('asd', 4) < 1:
break

How to send image from openCV to a Web API

I have a web camera and I can capture video from it as following:
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) and 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
What I want to do is to send the frame to an Web API (HTTP), receive the response image and show that image.
I'm new to openCV. Would you tell me how do I do this?
Try this (taken from a real project). Some parts (authentication, response validation) are obviously skipped. Hopefully it will give you a good grasp.
import cv2
from PIL import Image
from six import StringIO
import requests
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
cv2.imshow('frame', frame)
if cv2.waitKey(1) and 0xFF == ord('q'):
frame_im = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
pil_im = Image.fromarray(frame_im)
stream = StringIO()
pil_im.save(stream, format="JPEG")
stream.seek(0)
img_for_post = stream.read()
files = {'image': img_for_post}
response = requests.post(
url='/api/path-to-your-endpoint/',
files=files
)
break
cap.release()
cv2.destroyAllWindows()

Stream video from drone Parrot 2.0. Python + cv2

I am trying to have access to the stream of my drone's camera.
Here my code:
import cv2
import numpy
import libardrone
drone = libardrone.ARDrone()
cap = drone.image
while(True):
cap = drone.image
if not cap:
continue
ret, frame = convert
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
It does not work. It does not open any frame where I can see the stream video of my drone's camera.
What's wrong? Do you have any suggestions?
Thank you!
import cv2
cam = cv2.VideoCapture('tcp://192.168.1.1:5555')
running = True
while running:
# get current frame of video
running, frame = cam.read()
if running:
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == 27:
# escape key pressed
running = False
else:
# error reading frame
print 'error reading video feed'
cam.release()
cv2.destroyAllWindows()
Try this code...This works for me.

Categories