Stream edited images of PiCam to Youtube using Python - python

I'm working on project were I have a Raspberry Pi (Running Raspbian Jessie) with a camera (PiCam or a USB webcam) and I want to stream the images from the cam to Youtube and edit the images in realtime using OpenCV (add text and a logo) but I just can't figure it out. The fact that I'm a novice in Unix and Python doesn't help either.
I am able to edit each frame of the video stream using the following python code
import cv2, sys
import time
capture = cv2.VideoCapture(0)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
while True:
rc, img = capture.read()
if not rc:
continue
#make a copy of the image to add the semi-transparant bar
overlay = img.copy()
cv2.rectangle(overlay, (0,700), (1280, 720), (1,1,1),-1)
cv2.addWeighted(overlay, 0.75, img, 0.25, 0, img)
#add the text
imageText = "[{}]".format(time.strftime("%d %b %Y %H:%M:%S", time.localtime()))
cv2.putText(img, imageText, (10, 713), cv2.FONT_HERSHEY_SIMPLEX, 0.4,(255,255,255))
But now I have to add these frames to a output stream and send it to the rtmp stream of Youtube in the right format. I've been searching the internet and documentation all day but I can't seem to find a answer that does the job. I really can't see the wood for the trees...
Has anybody got a solution or can point me in the right direction.

Related

Cannot view video after writing in python/OpenCV

Edit: The problem was fixed after using VLC media player. The standard windows media player could not read the videos.
I am attempting to take a video, run it through an object detector, add the bounding boxes, and output the video with the bounding boxes. However, I'm having a strange problem where the video I output cannot be viewed. I get a "Server execution failed" error when attempting to play the video on my local windows 10 machine. I am developing through SSH on ubuntu 20.04 with the vs code remote development extension.
Here is some OpenCV code that does not work with my setup. The video is written to the disk, and OpenCV is able to read frames from output5.avi, however the output5.avi file cannot be opened as I described.
import cv2
cap = cv2.VideoCapture("video.mov")
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('output5.avi', fourcc, 30, (width, height), isColor=True)
while cap.isOpened():
# get validity boolean and current frame
ret, frame = cap.read()
# if valid tag is false, loop back to start
if not ret:
break
else:
frame = cv2.resize(frame, (width, height))
out.write(frame)
cap.release()
out.release()
I have also attempted to save the video through torchvision.io.write_video, but the exact same problem occurs. Tensorboard similarly doesn't work.
It must be something wrong with how the remote machine is set up, but I have no idea what could be wrong.

python - OpenCV VideoWriter outputs invalid videos and with different file sizes

I am simply trying to read a video using openCV Video Capture, and then outputting that same video using Video Writer. But the resultant video is not playable and each time I run the function, although having the same input video the output video has different file sizes.
cap = cv2.VideoCapture("video_input.mp4")
fps = cap.get(cv2.CAP_PROP_FPS)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
out = cv2.VideoWriter("video_output.mp4",cv2.VideoWriter_fourcc(*'mp4v'), fps, (int(width), int(height)))
count_frame = 0
if (cap.isOpened()== False):
print("Error opening video stream or file")
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
out.write(frame)
count_frame = count_frame + 1
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
Note: The input video has 8000 frames, if i use a condition to only write the first 2000 frames the video writer outputs those 2000 frames in a video without problem.
Does anyone know what is my problem? This exact code use to work fine some weeks ago.
Edit: I would also like to add that I´m running this code on a virtual machine on jupyterLab. But when i try to run in other virtual machine, it runs perfectly fine. The openCV versions are the same in both VM's

Python cv2.VideoCapture() has wrong resolution and read cropped images

I'm trying to read images from an IDS GV-5240CP camera plugged to my laptop via ethernet using Python and OpenCV.
This is what I am supposed to get :
A 1280x1024 image (here resized for upload)
But using this code:
import cv2
cap = cv2.VideoCapture(1)
_, frame = cap.read()
print(frame.shape)
cv2.imshow("Out", frame)
cv2.waitKey(2000)
cap.release()
cv2.destroyAllWindows()
cv2.imwrite('Test2.png', frame)
I get:
A 640x480 cropped image
How can I set my video capture to the native resolution?
VideoCapture uses 640x480 by default.
If you want a different resolution, specify it in the constructor or use the set() method with CAP_PROP_FRAME_WIDTH and so on. Details in the docs.
cap = cv2.VideoCapture(1, apiPreference=cv2.CAP_ANY, params=[
cv2.CAP_PROP_FRAME_WIDTH, 1280,
cv2.CAP_PROP_FRAME_HEIGHT, 1024])
out = cv2.VideoWriter(("E:", 'output.avi'), fourcc, 20.0, (640, 480))
try using this and change the resolution according to your need,
tho it will download or use
cv2.resize(frame,(w,h),fx=0,fy=0, interpolation = cv2.INTER_CUBIC)
since you don't want to do this then use set() module in OpenCV to set specific resolution. docs.opencv.org/4.x/d8/dfe/… this link for the full info about set()

Python/OpenCV camera capture resulting in blank/black images

I have a webcam and I have installed the camera drivers and it is using the vendor's drivers (not UVC). I am trying to capture an image and it is resulting in an array of zeros (frame). I have used another software on windows and it is able to get the video without issues. Any ideas on how to fix this issue?
import cv2
cam = cv2.VideoCapture(1)
f1 = cam.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
f2 = cam.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
f2 = cam.set(cv2.CAP_PROP_FPS, 1)
print(cam.get(3),cam.get(4))
if cam.isOpened():
_,frame = cam.read()
cv2.imshow('',frame)
Edit: Please check the comments. I tried a few suggestions and they didn't work. I changed the camera, but with the new camera, I get noise.

using opencv with zbar in python on windows 8.1 to detect qr codes

I am using opencv version 3.1.0 with zbar (latest version as of this post) and PIL (latest version as of this post)
import zbar
import Image
import cv2
# create a reader
scanner = zbar.ImageScanner()
# configure the reader
scanner.parse_config('enable')
#create video capture feed
cap = cv2.VideoCapture(0)
while(True):
ret, cv = cap.read()
cv = cv2.cvtColor(cv, cv2.COLOR_BGR2RGB)
pil = Image.fromarray(cv)
width, height = pil.size
raw = pil.tostring()
# wrap image data
image = zbar.Image(width, height, 'Y800', raw)
# scan the image for barcodes
scanner.scan(image)
# extract results
for symbol in image:
# do something useful with results
print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data
# clean up
print "/n ...Done"
I don't understand why this is not working it is supposed to constantly check for qrcodes in the current frame of the Video Stream and if it sees one it decodes it and prints what it says inside I hold up printed out qrcodes in front of my webcam and it does not work it shows that my camera is on and that there is a video stream occurring so somewhere in the while loop something is going wrong
I tried it before with qr codes on my computer not printed out and it worked fine
I also tried having it show me the current frame with cv2.imshow("out",cv) but when I did the program showed just a big grey square where it should show the video stream and then it froze so I had to kill Netbeans.
zbar works with grayscale images. Change cv = cv2.cvtColor(cv, cv2.COLOR_BGR2RGB) to cv = cv2.cvtColor(cv, cv2.COLOR_BGR2GRAY).
I'm guessing you're using this example code to base your program off of. They do the color to grayscale conversion with convert('L') on line 15.

Categories

Resources