- OpenCV 4 with Python Blueprints
- Dr. Menua Gevorgyan Arsen Mamikonyan Michael Beyeler
- 380字
- 2021-06-24 16:50:03
Running the app and main function routine
The chapter2.py script is responsible for running the app, and it first imports the following modules:
import cv2
import numpy as np
from gestures import recognize
from frame_reader import read_frame
The recognize function is responsible for recognizing a hand gesture, and we will compose it later in this chapter. We have also placed the read_frame method that we composed in the previous section in a separate script, for convenience.
In order to simplify the segmentation task, we will instruct the user to place their hand in the center of the screen. To provide a visual aid for this, we create the following function:
def draw_helpers(img_draw: np.ndarray) -> None:
# draw some helpers for correctly placing hand
height, width = img_draw.shape[:2]
color = (0,102,255)
cv2.circle(img_draw, (width // 2, height // 2), 3, color, 2)
cv2.rectangle(img_draw, (width // 3, height // 3),
(width * 2 // 3, height * 2 // 3), color, 2)
The function draws a rectangle around the image center and highlights the center pixel of the image in orange.
All the heavy lifting is done by the main function, shown in the following code block:
def main():
for _, frame in iter(read_frame, (False, None)):
The function iterates over grayscale frames from Kinect, and, in each iteration, it covers the following steps:
- Recognize hand gestures using the recognize function, which returns the estimated number of extended fingers (num_fingers) and an annotated BGR color image, as follows:
num_fingers, img_draw = recognize(frame)
- Call the draw_helpers function on the annotated BGR image in order to provide a visual aid for hand placement, as follows:
draw_helpers(img_draw)
- Finally, the main function draws the number of fingers on the annotated frame, displays results with cv2.imshow, and sets termination criteria, as follows:
# print number of fingers on image
cv2.putText(img_draw, str(num_fingers), (30, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255))
cv2.imshow("frame", img_draw)
# Exit on escape
if cv2.waitKey(10) == 27:
break
So, now that we have the main script, you will note that the only function that we are missing is the recognize function. In order to track hand gestures, we need to compose this function, which we will do in the next section.
- CMDB分步構(gòu)建指南
- Mastering RabbitMQ
- Java面向?qū)ο笏枷肱c程序設(shè)計(jì)
- Microsoft Dynamics 365 Extensions Cookbook
- C/C++算法從菜鳥到達(dá)人
- INSTANT Sencha Touch
- Learn WebAssembly
- 青少年P(guān)ython編程入門
- 零基礎(chǔ)學(xué)Python網(wǎng)絡(luò)爬蟲案例實(shí)戰(zhàn)全流程詳解(入門與提高篇)
- UML 基礎(chǔ)與 Rose 建模案例(第3版)
- 從零開始學(xué)C語(yǔ)言
- Express Web Application Development
- HTML5秘籍(第2版)
- Java Web應(yīng)用開發(fā)項(xiàng)目教程
- Magento 2 Beginners Guide