ubuntu+anaconda+opencv使用cv2.VideoCapture时出错

本文探讨了使用Python和OpenCV读取本地视频文件时遇到的问题,即无法打开视频文件,但能够成功调用摄像头。文章提供了代码示例,并寻求解决方法。
import cv2
cap = cv2.VideoCapture('1.mp4')  
if False == cap.isOpened():  
     print('open video failed')  
else:  
    print('open video succeeded')

使用绝对路径也不行,打不开本地视频

import cv2
cap = cv2.VideoCapture(0)
if False == cap.isOpened():
    print('open video failed')
else:
    print('open video successded')

但是调用摄像头的时候却不会报错。

求解

python3.6+opencv3.1


import cv2 import base64 import requests import numpy as np from aip import AipOcr # 百度OCR配置(使用您提供的密钥) APP_ID = '118561481' API_KEY = 'GEY7zfA4jn0osfQ2zOudRn' SECRET_KEY = 'VkLlThCb6jFn75cKceGCIp1PVIAnlkWh' client = AipOcr(APP_ID, API_KEY, SECRET_KEY) # 初始化摄像头 cap = cv2.VideoCapture(0) # 默认摄像头索引0 def enhance_image(image): """图像增强处理""" # CLAHE对比度受限自适应直方图均衡化 clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8)) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) enhanced = clahe.apply(gray) return cv2.cvtColor(enhanced, cv2.COLOR_GRAY2BGR) def detect_id_card(frame): """身份证区域检测""" # Canny边缘检测 edges = cv2.Canny(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), 50, 150) # 寻找轮廓 contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) if not contours: return None # 筛选最大矩形轮廓 max_contour = max(contours, key=cv2.contourArea) peri = cv2.arcLength(max_contour, True) approx = cv2.approxPolyDP(max_contour, 0.02*peri, True) # 透视变换(假设检测到四边形) if len(approx) == 4: pts = np.float32([point[0] for point in approx]) # 定义目标四边形(身份证标准比例 85.6×54mm) width, height = 540, 340 dst = np.float32([[0,0], [width,0], [width,height], [0,height]]) # 计算透视变换矩阵 matrix = cv2.getPerspectiveTransform(pts, dst) warped = cv2.warpPerspective(frame, matrix, (width, height)) return warped return None def ocr_id_card(image): """调用百度OCR身份证识别""" # 转换图像为jpg格式 _, buffer = cv2.imencode('.jpg', image) img_base64 = base64.b64encode(buffer).decode() # 调用身份证识别接口 result = client.idcard(img_base64, "front") return result.get('words_result', {}) def draw_results(frame, results): """在图像上绘制识别结果""" if not results: return frame y = 30 for key in ['姓名', '公民身份号码', '性别', '民族', '出生', '住址']: if key in results: text = f"{key}: {results[key]['words']}" cv2.putText(frame, text, (10, y), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0,255,0), 2) y += 30 return frame # 主循环 while True: ret, frame = cap.read() if not ret: break # 实处理 processed = enhance_image(frame) id_card = detect_id_card(processed) if id_card is not None: # OCR识别 results = ocr_id_card(id_card) # 显示结果 frame = draw_results(frame, results) cv2.imshow('ID Card', id_card) cv2.imshow('Real-time Capture', frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() 运行代码提示E:\Python\python.exe "E:\Python study\产品视觉实验一·\大作业2.py" Traceback (most recent call last): File "E:\Python study\产品视觉实验一·\大作业2.py", line 97, in <module> cv2.imshow('Real-time Capture', frame) cv2.error: OpenCV(4.11.0) D:\a\opencv-python\opencv-python\opencv\modules\highgui\src\window.cpp:1301: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage' 怎么回事,修改代码
最新发布
05-14
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值