From 142c1d4b931640571504516e203cafaf45a2baff Mon Sep 17 00:00:00 2001 From: Sidharth Mohan Date: Mon, 1 Jul 2024 12:33:18 +0530 Subject: [PATCH 1/3] updated readme.md --- README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3e5561a..f5ac551 100644 --- a/README.md +++ b/README.md @@ -8,11 +8,11 @@ Hand gesture control of visual information system *Installing Virtual Environment* ```console -python -m pip install --user virtualenv +python3 -m pip install --user virtualenv ``` *Creating New Virtual Environment* ```console -python -m venv venv +python3 -m virtualenv venv ``` *Activating Virtual Environment* ```console @@ -20,15 +20,15 @@ source venv/bin/activate ``` *Upgrade PIP* ```console -python -m pip install --upgrade pip +python3 -m pip install --upgrade pip ``` *Installing Packages* ```console -python -m pip install -r requirements.txt +python3 -m pip install -r requirements.txt ``` ### How to run ```console -python app.py +python3 app.py ``` \ No newline at end of file From 615f00aecf2d37ce6499586b6ae2469519d37e88 Mon Sep 17 00:00:00 2001 From: Sidharth Mohan Date: Mon, 1 Jul 2024 12:56:48 +0530 Subject: [PATCH 2/3] added debug cam --- debug/find_camera_index.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 debug/find_camera_index.py diff --git a/debug/find_camera_index.py b/debug/find_camera_index.py new file mode 100644 index 0000000..fbc56f9 --- /dev/null +++ b/debug/find_camera_index.py @@ -0,0 +1,22 @@ +import cv2 + +def find_camera_indices(): + index = 0 + arr = [] + while True: + cap = cv2.VideoCapture(index) + if cap.read()[0]: + arr.append(index) + cap.release() + else: + cap.release() + break + index += 1 + return arr + +if __name__ == "__main__": + camera_indices = find_camera_indices() + if camera_indices: + print(f"Available camera indices: {camera_indices}") + else: + print("No cameras found.") From 8cefdb96f7e7db03a8c1b67deceaa456ddcbe34c Mon Sep 17 00:00:00 2001 From: Sidharth Mohan Date: Tue, 2 Jul 2024 11:59:27 +0530 Subject: [PATCH 3/3] Simplified gen_frames Function: Ensures that the webcam is accessed and frames are generated correctly. IP Address Printing: Added code to print the server's IP address. Error Handling for Webcam Access: Added error handling to provide clear messages if the webcam cannot be accessed. --- app.py | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/app.py b/app.py index 735607b..bc7a974 100644 --- a/app.py +++ b/app.py @@ -3,13 +3,14 @@ import time import torch import logging +import socket import numpy as np from PIL import Image from os.path import join from threading import Thread from collections import OrderedDict from flask_socketio import SocketIO, emit -from flask import Flask, render_template_string +from flask import Flask, render_template_string, Response from torchvision.transforms import Compose, CenterCrop, Normalize, ToTensor from utils import load_config, ConvColumn, setup_gpio, gpio_action, read_html_file @@ -44,7 +45,6 @@ socketio = SocketIO(app) current_page = {"page": pages[0]} - def accuracy(output, target, topk=(1,)): maxk = max(topk) batch_size = target.size(0) @@ -60,7 +60,6 @@ def accuracy(output, target, topk=(1,)): gesture_label_int = top_pred.item() return gesture_label_int, gesture_detected - def get_frame_names(frames): nclips = 1 is_val = False @@ -81,10 +80,9 @@ def get_frame_names(frames): diff = num_frames - num_frames_necessary if not is_val: offset = np.random.randint(0, diff) - frame_names = frame_names[offset : num_frames_necessary + offset : step_size] + frame_names = frame_names[offset:num_frames_necessary + offset:step_size] return frame_names - def load_model(config_path): config = load_config(config_path) model = ConvColumn(8) @@ -104,14 +102,12 @@ def load_model(config_path): print("No checkpoint found at '{}'".format(config["checkpoint"])) return model - @app.route("/page_content") def page_content(): page = current_page["page"] page_html = read_html_file(join("static", page)) return render_template_string(page_html) - def process_video_stream(model, device, transform): cap = cv2.VideoCapture(0) if not cap.isOpened(): @@ -192,8 +188,7 @@ def process_video_stream(model, device, transform): cap.release() cv2.destroyAllWindows() - -@app.route("/") +@app.route('/') def index(): page = current_page["page"] page_html = read_html_file(join("static", page)) @@ -218,13 +213,29 @@ def index(): """ ) +@app.route('/video_feed') +def video_feed(): + return Response(gen_frames(), mimetype='multipart/x-mixed-replace; boundary=frame') + +def gen_frames(): + cap = cv2.VideoCapture(0) + if not cap.isOpened(): + raise RuntimeError("Could not start video capture.") + while True: + success, frame = cap.read() + if not success: + break + ret, buffer = cv2.imencode('.jpg', frame) + frame = buffer.tobytes() + yield (b'--frame\r\n' + b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') + cap.release() @socketio.on("connect") def handle_connect(): page = current_page["page"] emit("page_change", {"page": page}) - if __name__ == "__main__": setup_gpio() model = load_model("config.json") @@ -239,4 +250,10 @@ def handle_connect(): video_thread = Thread(target=process_video_stream, args=(model, device, transform)) video_thread.daemon = True video_thread.start() - socketio.run(app, debug=True) + + # Print the IP address + hostname = socket.gethostname() + ip_address = socket.gethostbyname(hostname) + print(f"Server is running at http://{ip_address}:5001") + + socketio.run(app, host="0.0.0.0", port=5001, debug=True)