I am trying to make a CV project where I have to detect object in video and send it the flask web app. I was able to send the frames to the flask webapp using following code inside generate() function
# show image
ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
# result
some_more_data = [] # this data will contain more information of object in the frame
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
then we use another function to send it to flask
#app.route('/video')
def video():
return Response(generate_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
so, now I also want to send the some_more_data list to the flask..
How can I do that.. or do I have to do something different?
Related
I am trying to take a RTSP video input and apply analytics and show it as a live stream. I am using Open CV and PyFlask for this purpose.
I can manage to show the output successfully. However, I am having issues when I try to access the host from multiple devices.
If I access from single device, it's working like a charm but when I try to access from multiple devices at the same time, it's breaking.
RTSP = f'''rtsp://wowzaec2demo.streamlock.net/vod/mp4:BigBuckBunny_115k.mp4'''
# importing
import cv2
from flask import Flask, render_template, Response
app= Flask(__name__)
cap= cv2.VideoCapture(RTSP)
def gen_frames():
while True:
success, frame = cap.read()
ret, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
yield(b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
#app.route('/')
def index():
return render_template('index.html')
#app.route('/video')
def video():
return Response(gen_frames(), mimetype = 'multipart/x-mixed-replace; boundary=frame')
if __name__ == "__main__":
app.run(debug = False, host = '0.0.0.0', port = 8018)
Need some expert advice. Thanks in advance.
Note - index.html code is given below:
<!DOCTYPE html>
<html>
<body>
<h1>Live streaming</h1>
<div>
<img src="{{ url_for('video') }}"/>
</div>
</body>
</html>
I am receiving an image from angular front end to python flask
(That part is done without error)
Now I am unable to send it to frontend angular, I need code
for both front end angular,html and flask
my back end
def readb64(base64_decoded):
buf = BytesIO()
buf.write(base64_decoded)
pimg = Image.open(buf)
return cv2.cvtColor(np.array(pimg), cv2.COLOR_RGB2BGR)
#app.route("/sync_frame", methods=['post'])
def form():
header, encoded = request.data.decode("utf-8").split(",", 1)
data2 = b64decode(encoded)
img = readb64(data2)
# Some modifications
return ??How to return??
I'm making a simple video stream to browser app on my Rpi.
I would like to know if there is a way to open a camera only when access the flask page and close the camera if page is not seen anymore. I don't want to let camera open whole time.
I was trying something like this, but it would involve button press (POST request) to flask server every time I want to close the tab what is little bit awkward.
from flask import Flask, render_template, Response, request
import cv2
from time import sleep
app = Flask(__name__)
CAMERA = None
def gen_frames():
global CAMERA
CAMERA = cv2.VideoCapture(0)
sleep(3)
CAMERA.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
CAMERA.set(cv2.CAP_PROP_FRAME_HEIGHT, 960)
while True:
success, frame = CAMERA.read()
if not success:
break
else:
_, buffer = cv2.imencode('.jpg', frame)
frame = buffer.tobytes()
# concat frame one by one and show result
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
#app.route('/video_feed')
def video_feed():
return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame')
#app.route('/', methods = ['POST', 'GET'])
def index():
global CAMERA
if request.method == 'POST':
if request.form['webcam']:
if CAMERA != None and CAMERA.isOpened():
CAMERA.release()
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=5000)
I have created a simple live video stream from the raspberry camera. It looks like this
server.py
from flask import Flask
from flask import render_template
from flask import Response
import cv2
app = Flask(__name__)
#app.route('/')
def index():
return render_template('index.html')
#app.route('/video_feed')
def video_feed():
return Response(gen(), mimetype='multipart/x-mixed-replace; boundary=frame')
def gen():
camera = cv2.VideoCapture(0)
while True:
ret, img = camera.read()
if ret:
frame = cv2.imencode('.jpg', img)[1].tobytes()
yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
else:
break
app.run(host='192.168.0.241', port=7070, debug=True)
index.html
<html>
<head>
<title>PiCamera stream</title>
</head>
<body>
<h1>Streaming</h1>
<img src="{{ url_for('video_feed') }}">
</body>
</html>
Everything works correct, I enter http://<raspberry_ip>:<port> in the browser and I can see the video.
Right now I need to create mobile app for watching this video, however I am struggling how to do it. Is there a way to capture video stream in iOS app?
Your video is streaming on
http://<raspberry_ip>:<port>/video_feed
http://<raspberry_ip>:<port>
Using Ngrok if you want to push your app to public server!
http://ngrok.com/
This question already has answers here:
I'm trying to use a simple webcam capture upload, upload isn't working?
(2 answers)
How do i take picture from client side(html) and save it to server side(Python)
(2 answers)
How can I capture an image via the user's webcam using getUserMedia?
(3 answers)
Closed 4 years ago.
First that all sorry about my not so good English skills and i appreciate the corrections.
I have tried to build a python program that can access the camera through a Flask web service, this is the code:
from flask import Flask, render_template, Response
import cv2
app = Flask(__name__)
#app.route('/')
def index():
return render_template('index.html')
def gen():
i = 1
while i < 10:
yield (b'--frame\r\n'
b'Content-Type: text/plain\r\n\r\n' + str(i) + b'\r\n')
i += 1
def get_frame():
camera_port = 0
ramp_frames = 100
camera = cv2.VideoCapture(camera_port) # this makes a web cam object
i = 1
while True:
retval, im = camera.read()
imgencode = cv2.imencode('.jpg', im)[1]
stringData = imgencode.tostring()
yield (b'--frame\r\n'
b'Content-Type: text/plain\r\n\r\n' + stringData + b'\r\n')
i += 1
del (camera)
#app.route('/calc')
def calc():
return Response(get_frame(), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=False, threaded=True)
And the html code:
<html>
<head>
<title>Video Streaming Demonstration</title>
</head>
<body>
<h1>Video Streaming Demonstration</h1>
<img src="{{ url_for('calc') }}">
<!-- <h1>{{ url_for('calc') }}</h1> -->
</body>
</html>
The problem i have with this is when I run it in my laptop and Flask deploys. I access the service from a different device doesn't use the device's cam, but activates my laptop's webcam.
Is it possible to make the service use the camera of the device from which I access it(client) instead the laptop's(server) camera?