srivatsavdamaraju commited on
Commit
79632d8
·
verified ·
1 Parent(s): a2e5257

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import base64
4
+ from flask import Flask, render_template
5
+ from flask_socketio import SocketIO, emit
6
+ from io import BytesIO
7
+ from PIL import Image
8
+
9
+ # Initialize Flask app and SocketIO
10
+ app = Flask(__name__)
11
+ socketio = SocketIO(app)
12
+
13
+ @app.route('/')
14
+ def index():
15
+ """Serve the HTML page."""
16
+ return render_template('index2.html')
17
+
18
+ @socketio.on('video_frame')
19
+ def handle_video_frame(data):
20
+ """Receive video frame from client, apply Canny edge detection, and emit the processed frame."""
21
+ # Extract the base64 image from the client
22
+ frame_data = data['image']
23
+
24
+ # Decode the base64 image to bytes
25
+ img_data = base64.b64decode(frame_data)
26
+
27
+ # Convert the image data to a numpy array
28
+ np_arr = np.frombuffer(img_data, np.uint8)
29
+ frame = cv2.imdecode(np_arr, cv2.IMREAD_COLOR)
30
+
31
+ if frame is not None:
32
+ # Apply Canny edge detection
33
+ edges = cv2.Canny(frame, 100, 200)
34
+
35
+ # Convert the edges image to BGR (for compatibility with base64 encoding)
36
+ edges_bgr = cv2.cvtColor(edges, cv2.COLOR_GRAY2BGR)
37
+
38
+ # Convert the processed image (edges) to base64
39
+ _, buffer = cv2.imencode('.jpg', edges_bgr)
40
+ edges_str = base64.b64encode(buffer).decode('utf-8')
41
+
42
+ # Emit the processed frame back to the client
43
+ emit('display_frame', {'image': edges_str}, broadcast=True)
44
+
45
+ if __name__ == '__main__':
46
+ socketio.run(app, host='0.0.0.0', port=5000)