- OpenCV 4 with Python Blueprints
- Dr. Menua Gevorgyan Arsen Mamikonyan Michael Beyeler
- 293字
- 2021-06-24 16:50:06
Running the app – the main() function routine
To run our app, we will need to execute the main() function routine. The following steps show us the execution of main() routine:
- The function first accesses the webcam with the VideoCapture method by passing 0 as an argument, which is a reference to the default webcam. If it can not access the webcam, the app will be terminated:
import cv2 as cv
from feature_matching import FeatureMatching
def main():
capture = cv.VideoCapture(0)
assert capture.isOpened(), "Cannot connect to camera"
- Then, the desired frame size and frame per second of the video stream is set. The following snippet shows the code for setting the frame size and frame per second of the video:
capture.set(cv.CAP_PROP_FPS, 10)
capture.set(cv.CAP_PROP_FRAME_WIDTH, 640)
capture.set(cv.CAP_PROP_FRAME_HEIGHT, 480)
- Next, an instance of the FeatureMatching class is initialized with a path to a template (or training) file that depicts the object of interest. The following code shows the FeatureMatching class:
matching = FeatureMatching(train_image='train.png')
- After that, to process the frames from the camera, we create an iterator from the capture.read function, which will terminate when the function fails to return frame ((False,None)). This can be seen in the following code block:
for success, frame in iter(capture.read, (False, None)):
cv.imshow("frame", frame)
match_succsess, img_warped, img_flann = matching.match(frame)
In the previous code block, the FeatureMatching.match method processes the BGR image (capture.read returns frame in BGR format). If the object is detected in the current frame, the match method will report match_success=True and return the warped image as well as the image that illustrates the matches—img_flann.
Let's move on and display the results in which our match method will return.
推薦閱讀
- 現(xiàn)代C++編程:從入門到實(shí)踐
- 深入理解Android(卷I)
- Android Jetpack開發(fā):原理解析與應(yīng)用實(shí)戰(zhàn)
- 深度學(xué)習(xí)經(jīng)典案例解析:基于MATLAB
- Clojure for Domain:specific Languages
- 高級C/C++編譯技術(shù)(典藏版)
- 概率成形編碼調(diào)制技術(shù)理論及應(yīng)用
- Drupal 8 Module Development
- 精通MATLAB(第3版)
- Haskell Data Analysis Cookbook
- 零基礎(chǔ)學(xué)C語言第2版
- Android群英傳
- Django 5企業(yè)級Web應(yīng)用開發(fā)實(shí)戰(zhàn)(視頻教學(xué)版)
- Python數(shù)據(jù)可視化之美:專業(yè)圖表繪制指南(全彩)
- AI自動化測試:技術(shù)原理、平臺搭建與工程實(shí)踐