Last active
January 21, 2021 21:08
-
-
Save piyush01123/d01ea811517bbc7d425d1ebd01ab271d to your computer and use it in GitHub Desktop.
Live stream camera feed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import flask | |
app = flask.Flask("CamApp") | |
def gen(): | |
cap = cv2.VideoCapture(0) | |
while True: | |
_, frame = cap.read() | |
yield (b'--frame\r\n' | |
b'Content-Type: image/jpeg\r\n\r\n' + cv2.imencode('.jpg', frame)[1].tostring() + b'\r\n') | |
@app.route('/') | |
def feed(): | |
return flask.Response(gen(), mimetype='multipart/x-mixed-replace; boundary=frame') | |
if __name__ == '__main__': | |
app.run(host='0.0.0.0', port = 8000, debug=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, Response | |
import subprocess,random,os,time, cv2 | |
cmd = lambda n: "screencapture -f temp/{}.jpg".format(n) | |
cmdcv = "screencapture -f temp/screen.jpg" | |
app = Flask("Test") | |
def fun(): | |
while True: | |
subprocess.Popen(cmdcv.split(), subprocess.PIPE) | |
frame = cv2.imread('temp/screen.jpg') | |
yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + cv2.imencode('.jpg', frame)[1].tostring() + b'\r\n') | |
def gun(): | |
while True: | |
n = random.randint(1,10000) | |
subprocess.Popen(cmd(n).split(), subprocess.PIPE) | |
time.sleep(0.25) | |
# print(os.path.isfile('temp/{}.jpg'.format(n)), 'temp/{}.jpg'.format(n)) | |
yield b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + open('temp/{}.jpg'.format(n),'rb').read() + b'\r\n' | |
@app.route('/cv2') | |
def feed(): | |
return Response(fun(), mimetype='multipart/x-mixed-replace; boundary=frame') | |
@app.route('/') | |
def geed(): | |
return Response(gun(), mimetype='multipart/x-mixed-replace; boundary=frame') | |
@app.route('/pic') | |
def pic(): | |
n='screen' | |
bcd = b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + open('temp/{}.jpg'.format(n),'rb').read() + b'\r\n' | |
return Response(bcd,mimetype='multipart/x-mixed-replace; boundary=frame') | |
app.run('0.0.0.0',8000,debug=True) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To expose publicly, use a free port forwarding service such as ngrok