I have a simple python script with open cv, which takes in a video and does object detection on it using YOLO. My question is, how can I display the output to my website as a live stream.
Here is the python code, saving to output.avi.
import cv2
from darkflow.net.build import TFNet
import numpy as np
import time
import pafy
options = {
'model': 'cfg/tiny-yolo.cfg',
'load': 'bin/yolov2-tiny.weights',
'threshold': 0.2,
'gpu': 0.75
}
tfnet = TFNet(options)
colors = [tuple(255 * np.random.rand(3)) for _ in range(10)]
capture = cv2.VideoCapture()
capture.open("rtmp://888888888888888")
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
#capture = cv2.VideoCapture(url)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
while True:
stime = time.time()
ret, frame = capture.read()
if ret:
results = tfnet.return_predict(frame)
for color, result in zip(colors, results):
if result['label'] == 'person':
tl = (result['topleft']['x'], result['topleft']['y'])
br = (result['bottomright']['x'], result['bottomright']['y'])
label = result['label']
confidence = result['confidence']
text = '{}: {:.0f}%'.format(label, confidence * 100)
frame = cv2.rectangle(frame, tl, br, color, 5)
frame = cv2.putText(
frame, text, tl, cv2.FONT_HERSHEY_COMPLEX, 0.8, (0, 0, 0), 2)
out.write(frame)
cv2.imshow('frame', frame)
print('FPS {:.1f}'.format(1 / (time.time() - stime)))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
capture.release()
out.release()
cv2.destroyAllWindows()
You can now easily publish your OpenCV apps to the web using Streamlit, a Python library. Follow this tutorial to learn the process step by step. In this tutorial, you'll learn how to easily convert an OpenCV project into a web app that you can showcase.
Client-side Python Code: First, we will create the socket by specifying the address family and address type to the socket. socket() function. Now I will use the connect function to connect to the server socket. The host IP address and the port number will be the same as the server.
Instead of writing into a file you can stream the images over your local network using ffmpeg or GStreamer and use some player to show the stream. Or you can use a simple flask server and a html page to do that see here: https://blog.miguelgrinberg.com/post/video-streaming-with-flask
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With