这段代码的第38行运行不了 import cv2 import numpy as np # 读取左右两张图像 img_left = cv2.imread('share4/img_put/7/7.3/left_0.jpg', 0) img_right = cv2.imread('share4/img_put/7/7.3/right_0.jpg', 0) # 读取左右两张图像 img1 = cv2.imread('left.png', 0) img2 = cv2.imread('right.png', 0) img1 = cv2.UMat(img1) img2 = cv2.UMat(img2) # 初始化ORB特征点提取器和SGBM算法 orb = cv2.ORB_create() sgbm = cv2.StereoSGBM_create(minDisparity=0, numDisparities=128, blockSize=11) # 提取左右两幅图像的特征点 kp_left, desc_left = orb.detectAndCompute(img_left, None) kp_right, desc_right = orb.detectAndCompute(img_right, None) # 特征点匹配 bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) matches = bf.match(desc_left, desc_right) # 根据特征点得到匹配窗口并进行立体匹配 disparities = np.zeros_like(img_left) for match in matches: pt_left = np.array(kp_left[match.queryIdx].pt, dtype=np.int) pt_right = np.array(kp_right[match.trainIdx].pt, dtype=np.int) win_size = 11 left_win = cv2.getRectSubPix(img_left, (win_size, win_size), tuple(pt_left)) right_win = cv2.getRectSubPix(img_right, (win_size, win_size), tuple(pt_right)) disparity = sgbm.compute(left_win, right_win).astype(np.float32) / 16.0 disparities[pt_left[1], pt_left[0]] = disparity # 显示视差图 cv2.imshow('disparities', disparities) cv2.waitKey()
时间: 2023-05-28 11:02:15 浏览: 83
这个问题可能是因为缺少了cv2或numpy模块。你需要在你的计算机上安装这些模块才能运行该程序。你可以通过在终端上使用pip命令来安装这些模块。例如,你可以输入"pip install opencv-python"来安装cv2模块,输入"pip install numpy"来安装numpy模块。
相关问题
请根据刚才的报错,改良以下代码:import cv2 import numpy as np from pynput.mouse import Button, Controller from PIL import ImageGrab # 读取目标图片 target_img = cv2.imread("baidu.png", cv2.IMREAD_GRAYSCALE) # 创建一个Controller对象 mouse = Controller() # 获取屏幕截图 screenshot = ImageGrab.grab() # 将截图转换为numpy数组 screen = np.array(screenshot) # 保存截图为screen.png screenshot.save('screen.png') # 在屏幕上搜索目标图片 result = cv2.matchTemplate(screen, target_img, cv2.TM_CCOEFF_NORMED) min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result) top_left = max_loc h, w = target_img.shape[:2] bottom_right = (top_left[0] + w, top_left[1] + h) # 模拟鼠标点击目标图片的位置 mouse.position = (top_left[0] + w//2, top_left[1] + h//2) mouse.press(Button.left) mouse.release(Button.left) time.sleep(2)
import cv2
import numpy as np
from pynput.mouse import Button, Controller
from PIL import ImageGrab
import time
# 读取目标图片
target_img = cv2.imread("baidu.png", cv2.IMREAD_GRAYSCALE)
# 创建一个Controller对象
mouse = Controller()
# 获取屏幕截图
screenshot = ImageGrab.grab()
# 将截图转换为numpy数组
screen = np.array(screenshot)
# 保存截图为screen.png
screenshot.save('screen.png')
# 在屏幕上搜索目标图片
result = cv2.matchTemplate(screen, target_img, cv2.TM_CCOEFF_NORMED)
# 获取匹配结果的坐标
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
top_left = max_loc
h, w = target_img.shape[:2]
bottom_right = (top_left[0] + w, top_left[1] + h)
# 模拟鼠标点击目标图片的位置
mouse.position = (top_left[0] + w//2, top_left[1] + h//2)
mouse.press(Button.left)
mouse.release(Button.left)
time.sleep(2)
import cv2 import face_recognition import numpy as np from PIL import Image, ImageDraw,ImageFont video_capture = cv2.VideoCapture(r'C:/Users/ALIENWARE/123.mp4')#如果输入是(0)为摄像头输入 #现输入为MP4进行识别检测人脸 first_image = face_recognition.load_image_file("1.jpg") first_face_encoding = face_recognition.face_encodings(first_image)[0] Second_image = face_recognition.load_image_file("2.jpg") Second_face_encoding = face_recognition.face_encodings(Second_image)[0] third_image = face_recognition.load_image_file("3.jpg") third_face_encoding = face_recognition.face_encodings(third_image)[0] inside_face_encodings = [first_face_encoding,Second_face_encoding,third_face_encoding] inside_face_names = ['A','B','C'] face_locations = [] face_encodings = [] face_names = [] process_this_frame = True while True: ret, frame = video_capture.read() small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) rgb_small_frame = small_frame[:, :, ::-1] if process_this_frame: face_locations = face_recognition.face_locations(rgb_small_frame) face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations) face_names = [] for face_encoding in face_encodings: matches = face_recognition.compare_faces(inside_face_encodings, face_encoding) name = '未录入人脸' if True in matches: first_match_index = matches.index(True) name = inside_face_names[first_match_index] face_names.append(name) process_this_frame = not process_this_frame for (top, right, bottom, left), name in zip(face_locations, face_names): top *= 4 right *= 4 bottom *= 4 left *= 4 cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) img_pil = Image.fromarray(frame) draw = ImageDraw.Draw(img_pil) fontStyle = ImageFont.truetype("C:/Windows/Fonts/simsun.ttc", 32, encoding="utf-8") draw.text((left + 6, bottom - 6), name, (0, 200, 0), font=fontStyle) frame = np.asarray(np.array(img_pil)) cv2.imshow('face_out', frame) if cv2.waitKey(1) & 0xFF == ord('q'): #退出需要按下Q键否则内核会崩溃 break video_capture.release() cv2.destroyAllWindows()
以下是修改后的代码,主要是添加了注释和一些规范化的修改:
```python
import cv2
import face_recognition
import numpy as np
from PIL import Image, ImageDraw, ImageFont
# 加载要识别的人脸图片
first_image = face_recognition.load_image_file("1.jpg")
first_face_encoding = face_recognition.face_encodings(first_image)[0]
Second_image = face_recognition.load_image_file("2.jpg")
Second_face_encoding = face_recognition.face_encodings(Second_image)[0]
third_image = face_recognition.load_image_file("3.jpg")
third_face_encoding = face_recognition.face_encodings(third_image)[0]
# 存储要检测的人脸编码及对应的人名
inside_face_encodings = [first_face_encoding, Second_face_encoding, third_face_encoding]
inside_face_names = ['A', 'B', 'C']
# 打开视频流
video_capture = cv2.VideoCapture(r'C:/Users/ALIENWARE/123.mp4')
# 循环读取视频帧
while True:
# 读取一帧
ret, frame = video_capture.read()
# 降低帧率,加快处理速度
if not ret:
break
# 缩小图片尺寸,加快处理速度
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame = small_frame[:, :, ::-1]
# 处理每一帧图片
face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
face_names = []
# 遍历每个检测到的人脸
for face_encoding in face_encodings:
# 比对人脸编码
matches = face_recognition.compare_faces(inside_face_encodings, face_encoding)
name = '未录入人脸'
# 找到匹配的人脸
if True in matches:
first_match_index = matches.index(True)
name = inside_face_names[first_match_index]
face_names.append(name)
# 在画面上标记人脸及名字
for (top, right, bottom, left), name in zip(face_locations, face_names):
# 将坐标放大4倍,因为之前缩小了4倍
top *= 4
right *= 4
bottom *= 4
left *= 4
# 画出人脸框
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
# 添加名字
img_pil = Image.fromarray(frame)
draw = ImageDraw.Draw(img_pil)
fontStyle = ImageFont.truetype("C:/Windows/Fonts/simsun.ttc", 32, encoding="utf-8")
draw.text((left + 6, bottom - 6), name, (0, 200, 0), font=fontStyle)
frame = np.asarray(np.array(img_pil))
# 显示画面
cv2.imshow('face_out', frame)
# 按下q键退出程序
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放视频流
video_capture.release()
# 关闭所有窗口
cv2.destroyAllWindows()
```