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'
怎么回事,修改代码
最新发布