opencv使用
绘制图片
def main(args):
import cv2
import numpy
mat = numpy.zeros((300,400),dtype = numpy.uint8)
mat = cv2.cvtColor(mat,cv2.COLOR_GRAY2BGR)
cv2.imshow('main',mat)
cv2.waitKey()
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))
显示图片
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# showpic.py
#
import cv2
def main(args):
img = cv2.imread('/home/pi/Pictures/test.jpg', cv2.IMREAD_COLOR)
cv2.imshow('image', img)
cv2.waitKey()
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))
人脸识别
import cv2
img = cv2.imread("model.png")
faceCascade = cv2.CascadeClassifier("cascades/haarcascade_frontalface_default.xml")
faces = faceCascade.detectMultiScale(img, 1.3)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 5)
cv2.imshow("img", img)
cv2.waitKey()
cv2.destroyAllWindows()
人脸识别训练
import cv2
import numpy
photos = list()
labels = list()
photos.append(cv2.imread("face/summer1.png",0))
labels.append(0)
photos.append(cv2.imread("face/summer2.png",0))
labels.append(0)
photos.append(cv2.imread("face/summer3.png",0))
labels.append(0)
photos.append(cv2.imread("face/Elvis1.png",0))
labels.append(1)
photos.append(cv2.imread("face/Elvis2.png",0))
labels.append(1)
photos.append(cv2.imread("face/Elvis3.png",0))
labels.append(1)
test = cv2.imread("face/summer4.png",0)
names = {"0":"Summer","1":"Elvis"}
recognizer = cv2.face.EigenFaceRecognizer_create()
recognizer.train(photos,numpy.array(labels))
label,confidence = recognizer.predict(test)
print("confidence = " + str(confidence))
print(names[str(label)])
|