Skip to content

Commit

Permalink
- добавление файла конфигурации
Browse files Browse the repository at this point in the history
  • Loading branch information
vladislav-serdyuk committed Jul 26, 2024
1 parent a6c2014 commit c2df3db
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 55 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@

![GPL](https://www.gnu.org/graphics/gplv3-with-text-136x68.png)

## Настройка
## Начало работы
- Установите зависимости (pip install -r requirements.txt)
```commandline
pip install -r requirements.txt
```
- Измените templates/index.html | div > img > width так, чтобы расстояние между картинками было равно 6см
- Если нужно, измените config.json


camera.index: индекс камеры, для авто поиска используйте -1
other.debug: отладка
other.show_window: показ окна, при использаваниии на сервере отключить

## Использование
Запуск:
Expand Down
20 changes: 20 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"segmentor": {
"model": 1
},
"hand_detector": {
"static_mode": false,
"max_hands": 1,
"model_complexity": 1,
"detection_con": 0.7,
"min_track_con": 0.5
},
"camera": {
"index": -1
},
"other": {
"debug": true,
"hand_on_gui": false,
"show_window": true
}
}
117 changes: 63 additions & 54 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,31 @@
from HandTrackingModule import HandDetector
from flask import Flask, render_template, Response

print('init')
segmentor = SelfiSegmentation(model=1) # remove background
hand_detector = HandDetector(static_mode=False,
max_hands=1,
model_complexity=1,
detection_con=0.7,
min_track_con=0.5)
print('Hi, this is AR_headset_soft by Vlad Serdyuk\r\n'
'Project on github: https://github.com/vladislav-serdyuk/AR_headset')

with open('config.json') as config_file:
config = json.JSONDecoder().decode(config_file.read())

segmentor = SelfiSegmentation(model=config['segmentor']['model']) # remove background
hand_detector = HandDetector(static_mode=config['hand_detector']['static_mode'],
max_hands=config['hand_detector']['max_hands'],
model_complexity=config['hand_detector']['model_complexity'],
detection_con=config['hand_detector']['detection_con'],
min_track_con=config['hand_detector']['min_track_con'])
app = Flask(__name__) # server
index = 0
while True: # auto search webcam
cap = cv2.VideoCapture(index)
if cap.read()[0]:
break
cap.release()
index += 1
# cap = cv2.VideoCapture(0)

if config['camera']['index'] == -1:
index = 0
while True: # auto search webcam
cap = cv2.VideoCapture(index)
if cap.read()[0]:
break
cap.release()
index += 1
else:
cap = cv2.VideoCapture(config['camera']['index'])

app_buffer: list[str] = [] # see docs
message: list[str] = [''] # see docs
cam_image: np.ndarray | None = None
Expand All @@ -49,7 +58,7 @@
fingers_touch = [0] * 4 # Указ с большим ... мизинец с большим
fingers_up = [0] * 5 # Большой ... мизинец
landmark = [(0, 0)] * 21 # see open-cv
Apps = [] # [index] = app
Apps = [] # [index] = app object
windows_positions = {} # {app.id: index}


Expand All @@ -68,10 +77,33 @@ def video_feed() -> Response:
return Response(get_frame(), mimetype='multipart/x-mixed-replace; boundary=frame')


def get_frame():
"""
Получает изображение и отправляет клиенту
:return: Generator[bytes, Any, None]
"""
while True:
if result_image is not None:
# noinspection PyTypeChecker
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + cv2.imencode('.jpg', result_image)[1].tobytes() + b'\r\n')


def find_distance(p1: tuple, p2: tuple):
return max(abs(p1[0] - p2[0]), abs(p1[1] - p2[1]))


def load_apps():
with open('pkglist.json') as file:
Apps[:] = [
importlib.import_module('pkg.' + pkg['dir'] + '.run').App(fingers_up, fingers_touch, app_buffer, message,
landmark)
for pkg in json.JSONDecoder().decode(file.read()).values()]
windows_positions.clear()
for i, _app in enumerate(Apps):
windows_positions[_app.id] = i


def render_gui(frame: np.ndarray):
global gui_image
"""
Expand All @@ -98,8 +130,8 @@ def render_gui(frame: np.ndarray):
try:
gui(gui_img)
except Exception as e:
print('ERROR in app')
print(e)
print('[WARNING] ERROR in app')
print(type(e), e)

if hand_on_gui:
min_x = max(bbox[0] - 20, 0)
Expand All @@ -122,8 +154,8 @@ def render_gui(frame: np.ndarray):
try:
gui(gui_img)
except Exception as e:
print('ERROR in app')
print(e)
print('[WARNING] ERROR in app')
print(type(e), e)
gui_image = gui_img
process_message_for_system()

Expand All @@ -137,7 +169,7 @@ def process_message_for_system():
Apps[-1], Apps[pos] = Apps[pos], Apps[-1]
message[0] = ''
elif cmd == 'reload-apps':
print('reload apps')
print('[LOG] reload apps')
load_apps()
message[0] = ''

Expand Down Expand Up @@ -184,43 +216,20 @@ def update_gui_image_in_background():
render_gui(cam_image)


def get_frame():
"""
Получает изображение и отправляет клиенту
:return: Generator[bytes, Any, None]
"""
while True:
if result_image is not None:
# noinspection PyTypeChecker
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + cv2.imencode('.jpg', result_image)[1].tobytes() + b'\r\n')


def load_apps():
with open('pkglist.json') as file:
Apps[:] = [
importlib.import_module('pkg.' + pkg['dir'] + '.run').App(fingers_up, fingers_touch, app_buffer, message,
landmark)
for pkg in json.JSONDecoder().decode(file.read()).values()]
windows_positions.clear()
for i, _app in enumerate(Apps):
windows_positions[_app.id] = i


# settings
debug = True
hand_on_gui = False
show_window = True
debug = config['other']['debug']
hand_on_gui = config['other']['hand_on_gui']
show_window = config['other']['show_window']

if __name__ == '__main__':
print('load apps')
print('[LOG] load apps')
load_apps()
print('start "update result image" daemon')
update_result_image_in_background_thread = Thread(target=update_result_image_in_background, daemon=True)
update_result_image_in_background_thread.start()
print('start "update gui image" daemon')
print('[LOG] start "update gui image" daemon')
update_gui_image_in_background_thread = Thread(target=update_gui_image_in_background, daemon=True)
update_gui_image_in_background_thread.start()
print('server start')
print('[LOG] start "update result image" daemon')
update_result_image_in_background_thread = Thread(target=update_result_image_in_background, daemon=True)
update_result_image_in_background_thread.start()
print('[LOG] starting server')
app.run(host='0.0.0.0') # server run
print('server stop')
print('[LOG] server is stopped')

0 comments on commit c2df3db

Please sign in to comment.