-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
78 lines (49 loc) · 1.85 KB
/
main.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
74
75
76
77
78
import cv2
import time
import imutils
import argparse
import numpy as np
from math import pow, sqrt
from imutils.video import FPS
from imutils.video import VideoStream
from flask import Flask, render_template, Response
#Initialize Flask
app = Flask(__name__)
@app.route('/')
def index():
return render_template('Direct_Stream.html')
def sketch(image):
# Convert image to grayscale
img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Clean up image using Guassian Blur
img_gray_blur = cv2.GaussianBlur(img_gray, (5,5), 0)
# Extract edges
canny_edges = cv2.Canny(img_gray_blur, 10, 70)
# Do an invert binarize the image
ret, mask = cv2.threshold(canny_edges, 70, 255, cv2.THRESH_BINARY_INV)
return mask
def gen():
#Initialize Video Stream (Use src = 0 for Webcam or src = 'path to video input')
print('[Status] Starting Video Stream...')
vs = VideoStream(src = 0).start()
#time.sleep(0.1)
fps = FPS().start()
#Loop Video Stream
while True:
#Resize Frame to 600 pixels
frame = vs.read()
frame = imutils.resize(frame, width=750,height=500)
frame=sketch(frame)
frame = cv2.imencode('.jpg', frame)[1].tobytes()
yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n')
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
fps.update()
fps.stop()
print("[INFO]Elapsed time: {:.2f}".format(fps.elapsed()))
print("[INFO]Approx. FPS: {:.2f}".format(fps.fps()))
@app.route('/video_feed')
def video_feed():
#Video streaming route. Put this in the src attribute of image tag in html code
return Response(gen(), mimetype='multipart/x-mixed-replace; boundary=frame')