To install opencv in python
pip install opencv-python
To install opencv in c++
git clone https://github.com/opencv/opencv.git mkdir -p build && cd build cmake ../opencv make -j4 sudo make install
CmakeLists.txt
cmake_minimum_required(VERSION 3.0) project(opencv_c__) find_package(OpenCV REQUIRED) include_directories(${OpenCV_INCLUDE_DIRS}) set(CMAKE_CXX_STANDARD 17) add_executable(opencv_c__ main.cpp) target_link_libraries(opencv_c__ ${OpenCV_LIBS})
I - Mouvement detection
def ex1(): cap = cv2.VideoCapture(0) object_detector = cv2.createBackgroundSubtractorMOG2() while True: ret, frame = cap.read() mask = object_detector.apply(frame) cv2.imshow('Video', mask) if cv2.waitKey(30) & 0xFF == 27: break cap.release() cv2.destroyAllWindows()
#include <iostream> #include <opencv2> #include <opencv2> #include <opencv2> #include <opencv2> #include <opencv2> int main() { cv::VideoCapture cap(0); cv::Ptr<:backgroundsubtractor> object_detector = cv::createBackgroundSubtractorMOG2(); while (true) { cv::Mat frame; cap >> frame; cv::Mat mask; object_detector->apply(frame, mask); cv::imshow("Video", mask); if (cv::waitKey(30) == 27) { break; } } cap.release(); cv::destroyAllWindows(); return 0; } </:backgroundsubtractor></opencv2></opencv2></opencv2></opencv2></opencv2></iostream>
II - Blur faces
def ex2(): cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.1, 4) for (x, y, w, h) in faces: roi = frame[y:y + h, x:x + w] roi = cv2.GaussianBlur(roi, (23, 23), 30) frame[y:y + h, x:x + w] = roi cv2.imshow("gray", gray) if cv2.waitKey(30) & 0xFF == 27: break cap.release() cv2.destroyAllWindows()
#include <iostream> #include <opencv2> #include <opencv2> #include <opencv2> #include <opencv2> #include <opencv2> int main() { cv::VideoCapture cap(0); while(true) { cv::Mat frame; cap >> frame; cv::CascadeClassifier face_cascade; face_cascade.load("haarcascade_frontalface_default.xml"); std::vector<:rect> faces; face_cascade.detectMultiScale(frame, faces, 1.1, 3, 0, cv::Size(30, 30)); for(int i = 0; i <h2> III - Tracing a movement </h2> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172592205430642.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="xample of computer vison technical test - Python / c++"><br> </p> <pre class="brush:php;toolbar:false">def ex3(): cap = cv2.VideoCapture(0) object_detector = cv2.createBackgroundSubtractorMOG2() last_coordinates = [] while True: ret, frame = cap.read() mask = object_detector.apply(frame) contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) for contour in contours: if cv2.contourArea(contour) <pre class="brush:php;toolbar:false">#include <iostream> #include <opencv2> #include <opencv2> #include <opencv2> #include <opencv2> #include <opencv2> int main() { cv::VideoCapture cap(0); cv::Ptr<:backgroundsubtractor> object_detector = cv::createBackgroundSubtractorMOG2(); std::vector<:rect> last_coordinates; while (true) { cv::Mat frame; cap >> frame; cv::Mat mask; object_detector->apply(frame, mask); std::vector<:vector>> contours; std::vector<:vec4i> hierarchy; cv::findContours(mask, contours, hierarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE); for (auto &contour : contours) { if (cv::contourArea(contour) </:vec4i></:vector></:rect></:backgroundsubtractor></opencv2></opencv2></opencv2></opencv2></opencv2></iostream>
The above is the detailed content of xample of computer vison technical test - Python / c++. For more information, please follow other related articles on the PHP Chinese website!

Python is an interpreted language, but it also includes the compilation process. 1) Python code is first compiled into bytecode. 2) Bytecode is interpreted and executed by Python virtual machine. 3) This hybrid mechanism makes Python both flexible and efficient, but not as fast as a fully compiled language.

Useaforloopwheniteratingoverasequenceorforaspecificnumberoftimes;useawhileloopwhencontinuinguntilaconditionismet.Forloopsareidealforknownsequences,whilewhileloopssuitsituationswithundeterminediterations.

Pythonloopscanleadtoerrorslikeinfiniteloops,modifyinglistsduringiteration,off-by-oneerrors,zero-indexingissues,andnestedloopinefficiencies.Toavoidthese:1)Use'i

Forloopsareadvantageousforknowniterationsandsequences,offeringsimplicityandreadability;whileloopsareidealfordynamicconditionsandunknowniterations,providingcontrolovertermination.1)Forloopsareperfectforiteratingoverlists,tuples,orstrings,directlyacces

Pythonusesahybridmodelofcompilationandinterpretation:1)ThePythoninterpretercompilessourcecodeintoplatform-independentbytecode.2)ThePythonVirtualMachine(PVM)thenexecutesthisbytecode,balancingeaseofusewithperformance.

Pythonisbothinterpretedandcompiled.1)It'scompiledtobytecodeforportabilityacrossplatforms.2)Thebytecodeistheninterpreted,allowingfordynamictypingandrapiddevelopment,thoughitmaybeslowerthanfullycompiledlanguages.

Forloopsareidealwhenyouknowthenumberofiterationsinadvance,whilewhileloopsarebetterforsituationswhereyouneedtoloopuntilaconditionismet.Forloopsaremoreefficientandreadable,suitableforiteratingoversequences,whereaswhileloopsoffermorecontrolandareusefulf

Forloopsareusedwhenthenumberofiterationsisknowninadvance,whilewhileloopsareusedwhentheiterationsdependonacondition.1)Forloopsareidealforiteratingoversequenceslikelistsorarrays.2)Whileloopsaresuitableforscenarioswheretheloopcontinuesuntilaspecificcond


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools
