Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of Python OpenCV Image recognition

Shulou Source: shulou.com Published: 2022-06-01 09:03:11 09月29日 Update

Editor to share with you the example analysis of Python OpenCV image recognition. I hope you will get something after reading this article. Let's discuss it together.

I. face recognition

There are two main implementation methods:

1. Haar cascade method: a traditional algorithm for face recognition.

Implementation steps:

Create a Haar cascade

Import a picture and turn it to gray

Call function interface for face recognition

Function prototype:

DetectMultiScale (img,scaleFactor,minNeighbors)

ScaleFactor: scaling siz

MinNeighbors: minimum pixel valu

Code case:

# create Haar cascade facer = cv2.CascadeClassifier ('. / haarcascades/haarcascade_frontalface_default.xml') # import face images and gray out img = cv2.imread ('p3.png') gray = cv2.cvtColor (img, cv2.COLOR_BGR2GRAY) # call the interface faces = facer.detectMultiScale (gray, 1.1,5) for (xmemyjieh) in faces: cv2.rectangle (img, (xjingy), (xonomw, yyogh), (0prit 0255) 2) cv2.imshow ('img', img) cv2.waitKey ()

Conclusion: the detection effect of Haar cascade method for complete face is good, but the effect for incomplete face recognition is poor, which may also be a defect of traditional algorithm, and its generalization ability is poor.

Extension: Haar cascade can also recognize detailed features in the face.

The code is as follows:

# create Haar cascade facer = cv2.CascadeClassifier ('. / haarcascades/haarcascade_frontalface_default.xml') eyer = cv2.CascadeClassifier ('. / haarcascades/haarcascade_eye.xml') # import face images and gray img = cv2.imread ('p3.png') gray = cv2.cvtColor (img, cv2.COLOR_BGR2GRAY) # call the interface faces = facer.detectMultiScale (gray, 1.1,5) I = 0for (x, img) in faces: cv2.rectangle (img, (x) Y), 2) ROI_img = img [y:y+h, x:x+w] eyes = eyer.detectMultiScale (ROI_img, 1.1,5) for) in eyes: cv2.rectangle (ROI_img, (xpeny), (xpenw, ymirh), (0min255), 2) I + = 1 name = 'img'+str (I) cv2.imshow (name) ROI_img) cv2.waitKey ()

Summary: the Haar cascade provides a variety of facial attributes recognition, including eyes, nose and mouth, but the effect is not necessarily so accurate.

Second, license plate recognition

Structure: Haar+Tesseract license plate recognition

Description: Haar cascade is only used to locate the location of the license plate, and Tesseract is used to extract the contents of the license plate.

Implementation steps:

1. Haar cascade locates the location of the license plate.

2. License plate preprocessing operation (binarization, morphology, filtering denoising, scaling)

3. Call Tesseract for character recognition

Note: Tesseract needs to be pre-installed here

Code case:

Import pytesseract# creates Haar concatenator carer = cv2.CascadeClassifier ('. / haarcascades/haarcascade_russian_plate_number.xml') # imports face images and grays img = cv2.imread ('chinacar.jpeg') gray = cv2.cvtColor (img, cv2.COLOR_BGR2GRAY) # call interface cars = carer.detectMultiScale (gray, 1.1,3) for (xrem) in cars: cv2.rectangle (img, (xmemy), (xpenw, yyogh), (0p0255) 2) # extract ROIroi = gray [y:y+h, x:x+w] # binary ret, roi_bin = cv2.threshold (roi, 0255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) # text recognition pytesseract.pytesseract.tesseract_cmd = r "D:\ Tesseract_OCR\ tesseract.exe" text = pytesseract.image_to_string (roi, lang='chi_sim+eng',config='--psm 8-- oem 3') print (text) cv2.putText (img, text, (20100) Cv2.FONT_HERSHEY_SIMPLEX, 2, (0Pie0255), 3) cv2.imshow ('img', img) cv2.waitKey ()

Conclusion: the location detection of license plate is more accurate, but the recognition of Tesseract is not so accurate, so it may be more accurate to use ORC. Of course, the accuracy of recognition is also related to the blur after image processing, and some processing can improve the recognition rate of characters.

III. DNN image classification

DNN is a deep neural network and is in the form of fully connected.

Note: OpenCV can use the DNN model, but it cannot be trained

Steps for using DNN:

Read the model and get the network structure

Read data (pictures or videos)

Convert the picture into a tensor and send it to the network

Model output result

Function prototype:

Import model: readNet (model, [config])

Image tensor: blobFromImage (image,scalefactor,size,mean,swapRB,crop)

Send to the network: net.setInput (blob)

Model reasoning: net.forward ()

Code case:

# Import model config = ". / model/bvlc_googlenet.prototxt" model = ". / model/bvlc_googlenet.caffemodel" net = dnn.readNetFromCaffe (config, model) # load picture Convert to tensor img = cv2.imread ('. / smallcat.jpeg') blob = dnn.blobFromImage (img, 1.0,1.0,( 224224), (104117123)) # Model reasoning net.setInput (blob) r = net.forward () idxs = np.argsort (r [0]) [::-1] [: 5] # Classification results show path ='. / model/synset_words.txt'with open (path) 'rt') as f: classes = [x [x.find (") + 1:] for x in f] for (I, idx) in enumerate (idxs): # display the results on the image if I = = 0: text =" Label: {}, {: .2f}% ".format (classes [idx], r [0] [idx] * 100) cv2.putText (img, text, (5,25) Cv2.FONT_HERSHEY_SIMPLEX, 0.7,0,255), 2) # display image cv2.imshow ("Image", img) cv2.waitKey (0)

After reading this article, I believe you have some understanding of "sample Analysis of Python OpenCV Image recognition". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Tags: Images pictures models faces license plates codes interfaces grayscale networks faces positions functions tensors effects text cases steps results examples analysis Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Docker Apple macOS NVidia