Skip to content

Commit

Permalink
stream error resolve--by mukesh
Browse files Browse the repository at this point in the history
  • Loading branch information
ofmukesh committed Aug 13, 2023
1 parent 2b2eed1 commit b15db73
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 85 deletions.
92 changes: 41 additions & 51 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
from flask_socketio import SocketIO, emit, join_room, leave_room
from flask import Flask, Response, render_template, session, request, make_response, redirect
from geventwebsocket.handler import WebSocketHandler
from auth.auth_decorators import login_required
from auth.auth_blueprint import auth_blueprint
from utils import generate_unique_number
from ai import GestureModel
import logging
import time
import os
import string
import random
import numpy as np
import base64
import cv2
from gevent import monkey
monkey.patch_all()
from flask import Flask, Response, render_template, request, make_response, redirect
import cv2
import base64
import numpy as np
import random
import string
import os
import time
import logging
from ai import GestureModel
from flask_socketio import SocketIO, emit
from utils import generate_unique_number
from auth.auth_blueprint import auth_blueprint
from auth.auth_decorators import login_required
from geventwebsocket.handler import WebSocketHandler

# Configure logging
logging.basicConfig(level=logging.DEBUG)
Expand All @@ -32,7 +32,7 @@
os.makedirs(PROCESSED_FRAMES_FOLDER, exist_ok=True)

app = Flask(__name__)
app.register_blueprint(auth_blueprint) # registering auth app blueprint
app.register_blueprint(auth_blueprint) # registering auth app blueprint
# add eventlet and gevent
socketio = SocketIO(app, async_mode='gevent', handler_class=WebSocketHandler)

Expand Down Expand Up @@ -64,57 +64,47 @@ def predict_frame(frames):
@app.route("/")
@login_required
def index():
user_id = session.get("user_id")
if not user_id:
session["user_id"] = str(time.time())
logger.info("Rendering index page")
return render_template("video_stream.html")

user_data = {}


@socketio.on("connect")
def handle_connect():
print("Client connected started",request.sid)

# user_sid = request.sid
# print("Client connected")
# user_data[user_sid] = {"frames": [], "unique_number": generate_unique_number(10)}
# print(f"Client with SID {user_sid} connected")
user_id = session.get("user_id")
join_room(user_id)
print("Client connected started", request.sid)


@socketio.on("disconnect")
def handle_disconnect():
user_sid = request.sid
if user_sid in user_data:
del user_data[user_sid]
print(f"Client with SID {user_sid} disconnected")
user_id = session.get("user_id")
leave_room(user_id)
print(f"Client with SID {user_id} disconnected")


user_data = {}


@socketio.on("process_frame")
def process_frame(data):
global frames
user_sid = request.sid
print(f"Processing frame for client with SID {user_sid} {request}")
if user_sid in user_data:
user_info = user_data[user_sid]
frame_data = data.get("frame", "")
if frame_data:
image_data = frame_data.split(",")[1]
decoded_data = base64.b64decode(image_data)
nparr = np.frombuffer(decoded_data, np.uint8)
frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
frames.append(frame)
if len(user_info["frames"]) == 5:
logger.info(f"Processing frame with unique number: {user_info}")
result = predict_frame(user_info["frames"])
user_info["frames"] = []
logger.info(
f"Frame processed successfully with unique frame number: {user_info['unique_number']}, result: {result}")
# Emit the frame_processed event only to the same client
emit("frame_processed", result, room=user_sid) # Emit to the specific client
logger.info(f"frame count {len(frames)}")

user_id = session.get("user_id")
frame_data = data.get("frame", "")
if frame_data:
image_data = frame_data.split(",")[1]
decoded_data = base64.b64decode(image_data)
nparr = np.frombuffer(decoded_data, np.uint8)
frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
user_data[user_id] = frame
if len(user_data.get(user_id, [])) == 720:
result = predict_frame(user_data[user_id])
user_data[user_id] = []
emit("frame_processed", {"prediction": result}, room=user_id)
logger.info(f"frame count {len(user_data.get(user_id, []))}")


if __name__ == "__main__":
host = os.getenv("HOST", "127.0.0.1")
socketio.run(app, host='0.0.0.0', port=8000,log_output=True)
socketio.run(app, host='0.0.0.0', port=8000, log_output=True)
44 changes: 10 additions & 34 deletions templates/video_stream.html
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ <h3>Welcome to Camera Streaming</h3><br>

<!-- scripts -->
<script>
let socket;
let socket = io();
const videoElement = document.getElementById('video');
const startButton = document.getElementById('startButton');
const stopButton = document.getElementById('stopButton');
Expand All @@ -166,10 +166,6 @@ <h3>Welcome to Camera Streaming</h3><br>
let stream;
let frameInterval;

function generateCustomSocketID() {
return Math.random().toString(36).substr(2, 9);
}

startButton.addEventListener('click', async () => {
streamThumbTxt.hidden = true
streamLoading.hidden = false
Expand All @@ -192,28 +188,13 @@ <h3>Welcome to Camera Streaming</h3><br>
try {
stream = await navigator.mediaDevices.getUserMedia(constraints);


// Attach the stream to the video element
videoElement.srcObject = stream;
socket = io();
console.log("socket",socket)
// Initialize socket connection with the user's session ID
// let userSid = socket && socket.id ? socket.id : null;
// if (userSid) {
// throw new Error('Unable to get user session ID');
// userSid= generateCustomSocketID();
// }
// socket = io({ query: { sid: userSid } });

if (!socket.connected) {
socket.connect();
}

startButton.classList.add('hidden');
stopButton.classList.remove('hidden');
stopButton.disabled = false;
console.log("socket.connected",socket,socket.connected)
if (socket.connected){
console.log("socket.connected", socket, socket.connected)
if (socket.connected) {
frameInterval = setInterval(sendFrame, 350);
}
streamLoading.hidden = true
Expand All @@ -224,7 +205,7 @@ <h3>Welcome to Camera Streaming</h3><br>
streamErr.innerText = "Error accessing camera: " + error
streamLoading.hidden = true
console.error('Error accessing camera:', error);
if (socket && socket.connected){
if (socket && socket.connected) {
socket.disconnect();
}
}
Expand All @@ -240,8 +221,8 @@ <h3>Welcome to Camera Streaming</h3><br>
stopButton.disabled = true;
startButton.classList.remove('hidden');
stopButton.classList.add('hidden');
// chatDisplay.innerHTML = ""
// chatDisplay.classList.add('hidden');
chatDisplay.innerHTML = ""
chatDisplay.classList.add('hidden');
streamThumbTxt.hidden = false
clearInterval(frameInterval);
// Close the socket connection
Expand All @@ -259,27 +240,22 @@ <h3>Welcome to Camera Streaming</h3><br>
const context = canvas.getContext("2d");
context.drawImage(videoElement, 0, 0, canvas.width, canvas.height);
const frameData = canvas.toDataURL("image/jpeg");

if (socket && socket.connected){
socket.emit("process_frame", {

socket.emit("process_frame", {
frame: frameData
});
}
});
}
}

// Listen for the "frame_processed" event from the backend
if (socket && socket.connected){
socket.on("frame_processed", result => {
socket.on("frame_processed", result => {
// Extract the "prediction" value from the result
const prediction = result.prediction;
const newEle = document.createElement("p")
newEle.innerText = "Prediction: " + prediction
chatDisplay.insertAdjacentElement("afterbegin", newEle);
chatDisplay.scrollTop = chatDisplay.scrollHeight;
});
}

</script>

<!-- Bootstrap JS and Popper.js CDN links -->
Expand Down

0 comments on commit b15db73

Please sign in to comment.