Skip to content

Commit 4a076c9

Browse files
author
Sergey Popov
committed
Facetracking and webcamera preview optimizations
1 parent bab927a commit 4a076c9

File tree

3 files changed

+54
-64
lines changed

3 files changed

+54
-64
lines changed

facetracker.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import traceback
66
import gc
77
import mediapipe as mp
8-
import pickle
98
mp_hands = mp.solutions.hands
109

1110
# mp_drawing = mp.solutions.drawing_utils
@@ -55,7 +54,6 @@
5554
parser.add_argument("--dump-points", type=str, help="When set to a filename, the current face 3D points are made symmetric and dumped to the given file when quitting the visualization with the \"q\" key", default="")
5655
parser.add_argument("--benchmark", type=int, help="When set to 1, the different tracking models are benchmarked, starting with the best and ending with the fastest and with gaze tracking disabled for models with negative IDs", default=0)
5756
parser.add_argument("--frame-data", type=int, help="When set to 1, the server is sending webcam frame data", default=0)
58-
parser.add_argument("--webcam-preview", type=int, help="When set to 1, the server is pop ups webcam preview window", default=0)
5957

6058
if os.name == 'nt':
6159
parser.add_argument("--use-dshowcapture", type=int, help="When set to 1, libdshowcapture will be used for video input instead of OpenCV", default=1)
@@ -133,7 +131,6 @@ def flush(self):
133131
sys.exit(0)
134132

135133
import math
136-
import pickle
137134
import numpy as np
138135
import time
139136
import cv2
@@ -142,8 +139,6 @@ def flush(self):
142139
import json
143140
from input_reader import InputReader, VideoReader, DShowCaptureReader, try_int
144141
from tracker import Tracker, get_model_base_path
145-
import pyvirtualcam
146-
from pyvirtualcam import PixelFormat
147142

148143
if args.benchmark > 0:
149144
model_base_path = get_model_base_path(args.model_dir)
@@ -161,7 +156,7 @@ def flush(self):
161156
print(1. / (total / 100.))
162157
sys.exit(0)
163158

164-
max_length = 65535
159+
max_length = 65535-28 # 28 is for UDP header
165160

166161

167162
target_ip = args.ip
@@ -224,8 +219,6 @@ def flush(self):
224219
source_name = input_reader.name
225220
if args.hands == 1:
226221
holistic = mp_hands.Hands(model_complexity=1,min_detection_confidence=0.82,min_tracking_confidence=0.82)
227-
if args.webcam_preview == 1:
228-
cam = pyvirtualcam.Camera(width, height, fps, fmt=PixelFormat.BGR, print_fps=fps)
229222

230223
while repeat or input_reader.is_open():
231224
if not input_reader.is_open() or need_reinit == 1:
@@ -272,9 +265,6 @@ def flush(self):
272265
tracker = Tracker(width, height, threshold=args.threshold, max_threads=args.max_threads, max_faces=args.faces, discard_after=args.discard_after, scan_every=args.scan_every, silent=False if args.silent == 0 else True, model_type=args.model, model_dir=args.model_dir, no_gaze=False if args.gaze_tracking != 0 and args.model != -1 else True, detection_threshold=args.detection_threshold, use_retinaface=args.scan_retinaface, max_feature_updates=args.max_feature_updates, static_model=True if args.no_3d_adapt == 1 else False, try_hard=args.try_hard == 1)
273266
if args.video_out is not None:
274267
out = cv2.VideoWriter(args.video_out, cv2.VideoWriter_fourcc('F','F','V','1'), args.video_fps, (width * args.video_scale, height * args.video_scale))
275-
if args.webcam_preview == 1:
276-
cam.send(frame)
277-
cam.sleep_until_next_frame()
278268

279269
try:
280270
inference_start = time.perf_counter()
@@ -428,8 +418,8 @@ def flush(self):
428418

429419

430420
if args.frame_data == 1:
431-
# frame = frame if width < 720 else cv2.resize(frame, (720, 480), interpolation=cv2.INTER_NEAREST)
432-
retval, buffer = cv2.imencode(".jpg", frame)
421+
cam_frame = frame if width <= 480 else cv2.resize(frame, (480, math.ceil(height * (480 / width))), interpolation=cv2.INTER_NEAREST)
422+
retval, buffer = cv2.imencode(".jpg", cam_frame)
433423
if retval:
434424
# convert to byte array
435425
buffer = buffer.tobytes()
@@ -550,8 +540,6 @@ def flush(self):
550540
print("Quitting")
551541
if args.hands == 1 and holistic is not None:
552542
holistic.close()
553-
if args.webcam_preview == 1 and cam is not None:
554-
cam.close()
555543
input_reader.close()
556544
if out is not None:
557545
out.release()

facetracker.spec

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,50 @@
11
# -*- mode: python ; coding: utf-8 -*-
22

3-
block_cipher = None
4-
53

6-
a = Analysis(['facetracker.py'],
7-
pathex=['C:\\OpenSeeFaceBuild'],
8-
binaries=[('dshowcapture/dshowcapture_x86.dll', '.'), ('dshowcapture/dshowcapture_x64.dll', '.'), ('dshowcapture/libminibmcapture32.dll', '.'), ('dshowcapture/libminibmcapture64.dll', '.'), ('escapi/escapi_x86.dll', '.'), ('escapi/escapi_x64.dll', '.'), ('run.bat', '.'), ('msvcp140.dll', '.'), ('vcomp140.dll', '.'), ('concrt140.dll', '.'), ('vccorlib140.dll', '.')],
9-
datas=[('mediapipe\modules\hand_landmark', 'mediapipe\modules\hand_landmark'), ('mediapipe\modules\palm_detection', 'mediapipe\modules\palm_detection')],
10-
hiddenimports=[],
11-
hookspath=[],
12-
runtime_hooks=[],
13-
excludes=['mpl-data', 'PyInstaller', 'pywt', 'skimage', 'scipy', 'pyinstaller'],
14-
win_no_prefer_redirects=False,
15-
win_private_assemblies=False,
16-
cipher=block_cipher,
17-
noarchive=False)
18-
19-
remove_bin = []
20-
for bin in a.binaries:
21-
if bin[0].startswith("opencv_video") or bin[0].startswith("PyInstaller"):
22-
remove_bin.append(bin[0])
23-
print(bin)
24-
remove_dat = []
25-
for bin in a.datas:
26-
if bin[0].startswith("opencv_video") or bin[0].startswith("PyInstaller"):
27-
remove_dat.append(bin[0])
28-
print("data ", bin)
4+
block_cipher = None
295

30-
a.binaries = [x for x in a.binaries if not x[0] in remove_bin]
31-
a.datas = [x for x in a.datas if not x[0] in remove_dat]
326

33-
pyz = PYZ(a.pure, a.zipped_data,
34-
cipher=block_cipher)
35-
exe = EXE(pyz,
36-
a.scripts,
37-
[],
38-
exclude_binaries=True,
39-
name='facetracker',
40-
debug=False,
41-
bootloader_ignore_signals=False,
42-
strip=False,
43-
upx=True,
44-
console=True )
7+
a = Analysis(
8+
['facetracker.py'],
9+
pathex=[],
10+
binaries=[('dshowcapture/*.dll', '.'), ('escapi/*.dll', '.'), ('venv/lib/site-packages/onnxruntime/capi/*.dll', 'onnxruntime\\capi'), ('venv/lib/site-packages/mediapipe/python/*.dll', 'mediapipe\\python'), ('msvcp140.dll', '.'), ('vcomp140.dll', '.'), ('concrt140.dll', '.'), ('vccorlib140.dll', '.'), ('run.bat', '.')],
11+
datas=[('mediapipe/modules/hand_landmark', '.'), ('mediapipe/modules/hand_landmark', '.'), ('mediapipe/modules/palm_detection', '.'), ('mediapipe/modules/palm_detection', '.')],
12+
hiddenimports=[],
13+
hookspath=[],
14+
hooksconfig={},
15+
runtime_hooks=[],
16+
excludes=[],
17+
win_no_prefer_redirects=False,
18+
win_private_assemblies=False,
19+
cipher=block_cipher,
20+
noarchive=False,
21+
)
22+
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
4523

46-
coll = COLLECT(exe,
47-
a.binaries,
48-
a.zipfiles,
49-
a.datas,
50-
strip=False,
51-
upx=True,
52-
upx_exclude=[],
53-
name='facetracker')
24+
exe = EXE(
25+
pyz,
26+
a.scripts,
27+
[],
28+
exclude_binaries=True,
29+
name='facetracker',
30+
debug=False,
31+
bootloader_ignore_signals=False,
32+
strip=False,
33+
upx=True,
34+
console=True,
35+
disable_windowed_traceback=False,
36+
argv_emulation=False,
37+
target_arch=None,
38+
codesign_identity=None,
39+
entitlements_file=None,
40+
)
41+
coll = COLLECT(
42+
exe,
43+
a.binaries,
44+
a.zipfiles,
45+
a.datas,
46+
strip=False,
47+
upx=True,
48+
upx_exclude=[],
49+
name='facetracker',
50+
)

make_exe.bat

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,24 @@ call venv\Scripts\activate.bat
1111
echo "Installing dependencies"
1212
pip install wheel
1313

14-
pip install onnxruntime opencv-python==4.5.4.60 pillow numpy==1.23.0 pyinstaller
14+
pip install onnxruntime opencv-python==4.5.4.60 pillow numpy==1.23.0 pyinstaller mediapipe=0.10.2
1515

1616
echo "Running pyinstaller"
1717
pyinstaller facetracker.py --clean ^
1818
--onedir ^
1919
--add-binary dshowcapture/*.dll;. ^
2020
--add-binary escapi/*.dll;. ^
2121
--add-binary venv/lib/site-packages/onnxruntime/capi/*.dll;onnxruntime\capi ^
22+
--add-binary venv/lib/site-packages/mediapipe/python/*.dll;mediapipe\python ^
2223
--add-binary msvcp140.dll;. ^
2324
--add-binary vcomp140.dll;. ^
2425
--add-binary concrt140.dll;. ^
2526
--add-binary vccorlib140.dll;. ^
26-
--add-binary run.bat;.
27+
--add-binary run.bat;. ^
28+
--add-data "mediapipe/modules/hand_landmark;." ^
29+
--add-data "mediapipe/modules/hand_landmark;." ^
30+
--add-data "mediapipe/modules/palm_detection;." ^
31+
--add-data "mediapipe/modules/palm_detection;."
2732

2833
echo "Deleting opencv dll"
2934
del dist\facetracker\cv2\opencv_videoio_*

0 commit comments

Comments
 (0)