本帖最后由 Vico 于 2023-11-22 17:32 编辑
当我手上拿到行空板之后,我感觉这个应用实在太过于广泛,其中openCV可以做很多视觉相关的应用。限于篇幅,在这里列举出10种常用的应用:
1.实时人脸检测: 使用OpenCV进行实时人脸检测。代码会启动摄像头,使用Haar级联分类器来检测和框选脸部
- import cv2
- face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
- cap = cv2.VideoCapture(0)
- while True:
- _, img = cap.read()
- gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- faces = face_cascade.detectMultiScale(gray, 1.1, 4)
- for (x, y, w, h) in faces:
- cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
- cv2.imshow('img', img)
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- cap.release()
- cv2.destroyAllWindows()
复制代码
2.运动检测: 检测和记录视频中的运动。可以用于安全监控系统。
- import cv2
- cap = cv2.VideoCapture(0)
- ret, frame1 = cap.read()
- ret, frame2 = cap.read()
- while cap.isOpened():
- diff = cv2.absdiff(frame1, frame2)
- gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
- blur = cv2.GaussianBlur(gray, (5,5), 0)
- _, thresh = cv2.threshold(blur, 20, 255, cv2.THRESH_BINARY)
- dilated = cv2.dilate(thresh, None, iterations=3)
- contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
- for contour in contours:
- (x, y, w, h) = cv2.boundingRect(contour)
- if cv2.contourArea(contour) < 900:
- continue
- cv2.rectangle(frame1, (x, y), (x+w, y+h), (0, 255, 0), 2)
- cv2.putText(frame1, "Status: {}".format('Movement'), (10,20), cv2.FONT_HERSHEY_SIMPLEX,
- 1, (0, 0, 255), 3)
- cv2.imshow("feed", frame1)
- frame1 = frame2
- ret, frame2 = cap.read()
- if cv2.waitKey(40) == 27:
- break
- cv2.destroyAllWindows()
- cap.release()
复制代码
3.条形码和二维码读取: 使用摄像头扫描并识别条形码和二维码。
- import cv2
- import pyzbar.pyzbar as pyzbar
- cap = cv2.VideoCapture(0)
- while True:
- _, frame = cap.read()
- decodedObjects = pyzbar.decode(frame)
- for obj in decodedObjects:
- cv2.putText(frame, str(obj.data), (50, 50), cv2.FONT_HERSHEY_SIMPLEX,
- 2, (255, 0, 0), 3)
- cv2.imshow("Frame", frame)
- key = cv2.waitKey(1)
- if key == 27:
- break
- cap.release()
- cv2.destroyAllWindows()
复制代码
4.颜色跟踪: 检测并跟踪视频中特定颜色的物体。
- import cv2
- import numpy as np
- cap = cv2.VideoCapture(0)
- while True:
- _, frame = cap.read()
- hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
- lower_red = np.array([30,150,50])
- upper_red = np.array([255,255,180])
- mask = cv2.inRange(hsv, lower_red, upper_red)
- res = cv2.bitwise_and(frame,frame, mask= mask)
- cv2.imshow('frame',frame)
- cv2.imshow('mask',mask)
- cv2.imshow('res',res)
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- cap.release()
- cv2.destroyAllWindows()
复制代码
5.手势识别: 通过摄像头
- import cv2
- import numpy as np
- cap = cv2.VideoCapture(0)
- while True:
- ret, frame = cap.read()
- # 转换为灰度图像
- gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
- # 应用阈值化来突出手势
- _, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
- # 查找轮廓并绘制
- contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
- for contour in contours:
- if cv2.contourArea(contour) > 1000:
- x, y, w, h = cv2.boundingRect(contour)
- cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
- cv2.imshow('Gesture', frame)
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- cap.release()
- cv2.destroyAllWindows()
复制代码
6.虚拟化妆: 检测面部特征并在脸部应用虚拟化妆。
- import cv2
- import dlib
- # 加载面部特征检测器
- detector = dlib.get_frontal_face_detector()
- predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
- cap = cv2.VideoCapture(0)
- while True:
- _, frame = cap.read()
- gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
- faces = detector(gray)
- for face in faces:
- landmarks = predictor(gray, face)
- # 用landmarks定位眼睛、嘴巴等,然后在上面绘制化妆效果
- # 示例:绘制眼线
- cv2.line(frame, (landmarks.part(36).x, landmarks.part(36).y),
- (landmarks.part(39).x, landmarks.part(39).y), (0, 0, 255), 2)
- cv2.imshow("Frame", frame)
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- cap.release()
- cv2.destroyAllWindows()
复制代码
7.车牌识别: 用于识别视频中的车牌号码。
- # 这需要复杂的逻辑和可能涉及机器学习模型,以下是简化的概览
- import cv2
- # 载入预训练的车牌识别模型
- plate_cascade = cv2.CascadeClassifier('haarcascade_russian_plate_number.xml')
- cap = cv2.VideoCapture(0)
- while True:
- _, img = cap.read()
- gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- plates = plate_cascade.detectMultiScale(gray, 1.1, 3)
- for (x, y, w, h) in plates:
- cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
- cv2.imshow('img', img)
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- cap.release()
- cv2.destroyAllWindows()
复制代码
8.虚拟试衣: 使用摄像头捕捉用户形象并实时试穿不同的服装。以下编程使用了一个静态的图像(如T恤)作为虚拟服装,并简化地假设用户位于摄像头正前方的固定位置。复制代码
9.实现眼动跟踪,可用于用户界面控制或者行为研究。
- import cv2
- import dlib
- # 加载面部特征检测器
- detector = dlib.get_frontal_face_detector()
- predictor = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat')
- cap = cv2.VideoCapture(0)
- while True:
- _, frame = cap.read()
- gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
- faces = detector(gray)
- for face in faces:
- landmarks = predictor(gray, face)
- left_eye = landmarks.part(36), landmarks.part(37), landmarks.part(38), landmarks.part(39), landmarks.part(40), landmarks.part(41)
- right_eye = landmarks.part(42), landmarks.part(43), landmarks.part(44), landmarks.part(45), landmarks.part(46), landmarks.part(47)
- # 绘制眼部轮廓
- for n in left_eye + right_eye:
- cv2.circle(frame, (n.x, n.y), 2, (255, 0, 0), -1)
- cv2.imshow("Eye Tracking", frame)
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- cap.release()
- cv2.destroyAllWindows()
复制代码
10.虚拟背景: 更换摄像头捕捉视频的背景,常用于视频会议或者直播。
- import cv2
- import numpy as np
- cap = cv2.VideoCapture(0)
- # 预设一个背景图片
- background = cv2.imread('background.jpg')
- while True:
- _, frame = cap.read()
- # 应用一些图像处理技术来分离前景和背景
- # 示例代码省略
- # 结合前景和新背景
- combined_frame = np.where(mask, frame, background)
- cv2.imshow("Virtual Background", combined_frame)
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- cap.release()
- cv2.destroyAllWindows()
复制代码
|