-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWebService.py
73 lines (52 loc) · 1.54 KB
/
WebService.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
import multiprocessing
import uuid
from multiprocessing import Process
from uuid import UUID
from flask import Flask, jsonify, json, stream_with_context, render_template
from flask_cors import CORS
from flask_restful import Api, Resource
import matplotlib.path as mplPath
from flask import Response
from threading import Thread
from time import sleep
import cv2
from tf_session.tf_session_utils import Pipe
cap = cv2.VideoCapture(-1)
pipe = Pipe()
def load():
fps = cap.get(cv2.CAP_PROP_FPS)
print("Frames Per Second:", fps, "\n")
while(True):
ret, image = cap.read()
if not ret:
try:
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
except:
pass
continue
pipe.push(image)
sleep(0.05)
app = Flask(__name__)
CORS(app)
def genVideoFeed():
Thread(target=load).start()
"""Video streaming generator function."""
while True:
try:
# cv2.imshow("image", image)
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + cv2.imencode('.jpg', pipe.pull(True)[1])[1].tostring() + b'\r\n')
except GeneratorExit:
return
except:
pass
@app.route('/')
def video_feed():
"""Video streaming route. Put this in the src attribute of an img tag."""
return Response(genVideoFeed(),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
print("in main")
Thread(target=load).start()
app.run()