Computer Vision with OpenCV: AI Image Recognition Basics
Computer Vision with OpenCV: AI Image Recognition Basics
💡 What You Will Learn
Computer Vision with OpenCV: AI Image Recognition Basics
|:----|:----|:--------|:----| | OpenCV | 4.10+ | pip install opencv-python | ~30MB | | HuggingFace Transformers | 4.48+ | pip install transformers | ~15MB | | PyTorch | 2.5+ | pip install torch ||
import cv2
import numpy as np
#
img = cv2.imread('photo.jpg')
print(f": {img.shape}") # (, , )
#
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Canny
edges = cv2.Canny(gray, 50, 150)
#
cv2.imshow('Original', img)
cv2.imshow('Edges', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('edges_output.jpg', edges)
from transformers import pipeline
# 500MB
classifier = pipeline("image-classification", model="google/vit-base-patch16-224")
result = classifier("photo.jpg")
print(result)
# [{'label': 'golden retriever', 'score': 0.95}, ...]
|:----|:-------------|:----------|:---------| ||| ResNet50/ViT>90% | +30% | || Haar Cascade~80% | MTCNN/RetinaFace>99% | +19% |
import cv2
from facenet_pytorch import MTCNN
import torch
device = 'cuda' if torch.cuda.is_available() else 'cpu'
detector = MTCNN(keep_all=True, device=device)
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
boxes, probs = detector.detect(frame)
if boxes is not None:
for box, prob in zip(boxes, probs):
if prob > 0.9:
x1, y1, x2, y2 = [int(v) for v in box]
cv2.rectangle(frame, (x1,y1), (x2,y2), (0,255,0), 2)
cv2.putText(frame, f'{prob:.2f}', (x1,y1-10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)
cv2.imshow('Face Detection', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
from paddleocr import PaddleOCR
ocr = PaddleOCR(use_angle_cls=True, lang='ch')
result = ocr.ocr('receipt.jpg')
for line in result[0]:
print(f": {line[1][0]}, : {line[1][1]:.2f}")
#
# : , : 0.98
# : : ยฅ1,280.00, : 0.96
Related Articles
2026-07-27
ComfyUI Workflow Tutorial 2026: Node-Based AI Image Generation
2026-08-06
Open Source LLM Platform: Open WebUI, LobeChat and Jan Compared
2026-07-17
AI Agent Compliance Audit 2026
Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only โ no paid placements.
