Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancements to gen_frames Function, Debug Cam Addition, and Documentation Update #3

Merged
merged 3 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ 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
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
```
39 changes: 28 additions & 11 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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():
Expand Down Expand Up @@ -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))
Expand All @@ -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")
Expand All @@ -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)
22 changes: 22 additions & 0 deletions debug/find_camera_index.py
Original file line number Diff line number Diff line change
@@ -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.")