Cov txheej txheem:

Kev Nrhiav Kom Ntsej Muag Opencv, Kev Qhia thiab Kev Paub: 3 Kauj Ruam
Kev Nrhiav Kom Ntsej Muag Opencv, Kev Qhia thiab Kev Paub: 3 Kauj Ruam

Video: Kev Nrhiav Kom Ntsej Muag Opencv, Kev Qhia thiab Kev Paub: 3 Kauj Ruam

Video: Kev Nrhiav Kom Ntsej Muag Opencv, Kev Qhia thiab Kev Paub: 3 Kauj Ruam
Video: Mathematics with Python! Evaluating Polynomials 2024, Kaum ib hlis
Anonim
Kev Tshawb Nrhiav Lub ntsej muag Opencv, Kev Qhia thiab Kev Paub
Kev Tshawb Nrhiav Lub ntsej muag Opencv, Kev Qhia thiab Kev Paub

OpenCV yog lub tsev qhib lub khoos phis tawj lub zeem muag pom uas nrov heev rau kev ua cov duab yooj yim ua haujlwm xws li qhov muag plooj, cov duab sib xyaw, txhim kho cov duab nrog rau cov vis dis aus zoo, thresholding thiab lwm yam Ntxiv nrog rau kev ua cov duab, nws muab ntau yam kev kawm ua ntej cov qauv uas tuaj yeem siv ncaj qha los daws cov haujlwm yooj yim ntawm tes.

rau kev teeb tsa opencv siv qhov txuas no

www.instructables.com/id/Opencv-and-Python…

Kauj Ruam 1: Tshawb Pom Lub Ntsej Muag Hauv Lub Sijhawm Yees Duab

koj tuaj yeem tshawb nrhiav google rau ntau lub program nrhiav lub ntsej muag thiab lub ntsej muag uas pom yuav tsum tau khaws tseg hauv daim nplaub tshev rau ntxiv cov duab ua tej yam xws li kev qhia thiab cim npe. peb yuav mus sau 30 qhov qauv

ua cv2

import numpy as np

import os import sys

lub koob yees duab = cv2. VideoCapture (0)

faceCascade = cv2. CascadeClassifier ("haarcascade_frontalface_default.xml") #ntxiv koj cov ntaub ntawv harcascade

name = raw_input ("Nws lub npe hu li cas?")

#tag nrho cov ntaub ntawv yuav raug khaws tseg hauv Cov Neeg Siv/prasad/Cov Ntaub Ntawv/duab nplaub tshev

dirName = "/Cov neeg siv/prasad/Cov ntaub ntawv/duab/" + lub npe

print (dirName) yog tias tsis os.path.exists (dirName): os.makedirs (dirName) print ("Tsim Cov Ntawv Teev Npe") lwm qhov: print ("Lub npe twb muaj lawm") sys.exit ()

suav = 1

#peb yuav sau 30 qhov qauv

thaum suav 30: so # thav duab = frame.array grey = cv2.cvtColor (thav duab, cv2. COLOR_BGR2GRAY) ntsej muag = faceCascade.detectMultiScale (grey, 1.5, 5) rau (x, y, w, h) hauv ntsej muag: roiGray = grey [y: y + h, x: x + w] fileName = dirName + "/" + name + str (count) + ".jpg" cv2.imwrite (fileName, roiGray) cv2.imshow ("ntsej muag", roiGray) cv2.rectangle (thav duab, (x, y), (x+w, y+h), (0, 255, 0), 2) suav+= 1 cv2.imshow ('thav duab', thav duab) qhov tseem ceeb = cv2.txogKey (1)

yog qhov tseem ceeb == 27:

so

#lub koob yees duab.release ()

cv2.destroyAllWindows ()

Kauj Ruam 2: Qhia Koj Cov Qauv Piv Txwv

Tom qab Kev Pom Lub ntsej muag tau ua tiav ces peb tuaj yeem mus rau kev qhia cov duab

import osimport numpy as np from PIL import Image import cv2 import pickle #import serial

#ser = serial. Serial ('/dev/ttyACM0', 9600, sijhawm = 1)

faceCascade = cv2. CascadeClassifier ("haarcascade_frontalface_default.xml")

lees paub = cv2.face. LBPHFaceRecognizer_create ()

baseDir = os.path.dirname (os.path.abspath (_ file_))

#qhia cov duab hauv qab cov ntawv tais ceev tseg

imageDir = os.path.join (baseDir, "duab")

currentId = 1

labelIds = {} yLabels = xTrain = #ser.write ("Kev cob qhia …" "encode ())

rau hauv paus, dirs, cov ntaub ntawv hauv os.walk (imageDir):

print (hauv paus, dirs, cov ntaub ntawv) rau cov ntaub ntawv hauv cov ntaub ntawv: luam tawm (cov ntaub ntawv) yog file.endswith ("png") lossis file.endswith ("jpg"): path = os.path.join (root, file) label = os.path.basename (hauv paus) luam tawm (daim ntawv lo)

yog tias tsis sau rau hauv labelIds:

labelIds [daim ntawv lo] = currentId luam tawm (labelIds) currentId += 1

id_ = labelIds [daim ntawv lo]

pilImage = Image.open (path).convert ("L") imageArray = np.array (pilImage, "uint8") face = faceCascade.detectMultiScale (imageArray, scaleFactor = 1.1, min Neighbors = 5)

rau (x, y, w, h) hauv ntsej muag:

roi = imageArray [y: y+h, x: x+w] xTrain.append (roi) yLabels.append (id_)

with open ("labels", "wb") as f:

pickle.dump (labelIds, f) f.close ()

recognizer.train (xTrain, np.array (yLabels))

recognizer.save ("trainer.yml") luam tawm (labelIds)

Kauj Ruam 3: Paub Lub ntsej muag

tom qab kev kawm tiav tam sim no koj tuaj yeem ua raws cov cai hauv qab no kom nws yuav pib lees paub koj lub ntsej muag uas tau kawm tiav

ntshuam osos.environ ['PYTHONINSPECT'] = 'on' import cv2 import numpy as np import pickle #import RPi. GPIO as GPIO from time import sleep

nrog qhib ('ntawv cim', 'rb') raws li f:

dicti = pickle.load (f) f.close ()

lub koob yees duab = cv2. VideoCapture (0)

faceCascade = cv2. CascadeClassifier ("haarcascade_frontalface_default.xml")

lees paub = cv2.face. LBPHFaceRecognizer_create () lees paub.read ("tus kws qhia.yml")

font = cv2. FONT_HERSHEY_SIMPLEX

kawg = ''

#rau thav duab hauv camera.capture_continuous (rawCapture, format = "bgr", use_video_port = True):

thaum muaj tseeb: ret, thav duab = camera.read () grey = cv2.cvtColor (thav duab, cv2. COLOR_BGR2GRAY) ntsej muag = faceCascade.detectMultiScale (grey, scaleFactor = 1.5, min Neighbors = 5) rau (x, y, w, h) hauv ntsej muag: roiGray = grey [y: y+h, x: x+w]

id_, conf = recognizer.predict (roiGray)

rau lub npe, tus nqi hauv dicti.items ():

yog tus nqi == id_: print (name) cv2.putText (thav duab, lub npe, (x, y), font, 2, (0, 0, 255), 2, cv2. LINE_AA) yog lub npe! = xeem: xeem = npe if conf <= 70: cv2.rectangle (thav duab, (x, y), (x+w, y+h), (0, 255, 0), 2)

cv2.imshow ('thav duab', thav duab)

qhov tseem ceeb = cv2.waitKey (1)

yog qhov tseem ceeb == 27:

tawg cv2.destroyAllWindows ()

Pom zoo: