puttextpython
1. 如何用python给头像加一个数字
这是图像处理的内容,其实就是在头像图片上加一个数字。python有很多图像处理库和模块,常用的有PIL和opencv。可以直接用opencv的putText函数添加一个数字,也可以用PIL的text方法
2. 谁用过python中的第三方库face recognition
简介
该库可以通过python或者命令行即可实现人脸识别的功能。使用dlib深度学习人脸识别技术构建,在户外脸部检测数据库基准(Labeled Faces in the Wild)上的准确率为99.38%。
在github上有相关的链接和API文档。
在下方为提供的一些相关源码或是文档。当前库的版本是v0.2.0,点击docs可以查看API文档,我们可以查看一些函数相关的说明等。
安装配置
安装配置很简单,按照github上的说明一步一步来就可以了。
根据你的python版本输入指令:
pip install face_recognition11
或者
pip3 install face_recognition11
正常来说,安装过程中会出错,会在安装dlib时出错,可能报错也可能会卡在那不动。因为pip在编译dlib时会出错,所以我们需要手动编译dlib再进行安装。
按照它给出的解决办法:
1、先下载下来dlib的源码。
git clone
2、编译dlib。
cd dlib
mkdir build
cd build
cmake .. -DDLIB_USE_CUDA=0 -DUSE_AVX_INSTRUCTIONS=1
cmake --build1234512345
3、编译并安装python的拓展包。
cd ..
python3 setup.py install --yes USE_AVX_INSTRUCTIONS --no DLIB_USE_CUDA1212
注意:这个安装步骤是默认认为没有GPU的,所以不支持cuda。
在自己手动编译了dlib后,我们可以在python中import dlib了。
之后再重新安装,就可以配置成功了。
根据你的python版本输入指令:
pip install face_recognition11
或者
pip3 install face_recognition11
安装成功之后,我们可以在python中正常import face_recognition了。
编写人脸识别程序
编写py文件:
# -*- coding: utf-8 -*-
#
# 检测人脸
import face_recognition
import cv2
# 读取图片并识别人脸
img = face_recognition.load_image_file("silicon_valley.jpg")
face_locations = face_recognition.face_locations(img)
print face_locations
# 调用opencv函数显示图片
img = cv2.imread("silicon_valley.jpg")
cv2.namedWindow("原图")
cv2.imshow("原图", img)
# 遍历每个人脸,并标注
faceNum = len(face_locations)
for i in range(0, faceNum):
top = face_locations[i][0]
right = face_locations[i][1]
bottom = face_locations[i][2]
left = face_locations[i][3]
start = (left, top)
end = (right, bottom)
color = (55,255,155)
thickness = 3
cv2.rectangle(img, start, end, color, thickness)
# 显示识别结果
cv2.namedWindow("识别")
cv2.imshow("识别", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
注意:这里使用了python-OpenCV,一定要配置好了opencv才能运行成功。
运行结果:
程序会读取当前目录下指定的图片,然后识别其中的人脸,并标注每个人脸。
(使用图片来自美剧硅谷)
编写人脸比对程序
首先,我在目录下放了几张图片:
这里用到的是一张乔布斯的照片和一张奥巴马的照片,和一张未知的照片。
编写程序:
# 识别图片中的人脸
import face_recognition
jobs_image = face_recognition.load_image_file("jobs.jpg");
obama_image = face_recognition.load_image_file("obama.jpg");
unknown_image = face_recognition.load_image_file("unknown.jpg");
jobs_encoding = face_recognition.face_encodings(jobs_image)[0]
obama_encoding = face_recognition.face_encodings(obama_image)[0]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]
results = face_recognition.compare_faces([jobs_encoding, obama_encoding], unknown_encoding )
labels = ['jobs', 'obama']
print('results:'+str(results))
for i in range(0, len(results)):
if results[i] == True:
print('The person is:'+labels[i])
运行结果:
识别出未知的那张照片是乔布斯的。
摄像头实时识别
代码:
# -*- coding: utf-8 -*-
import face_recognition
import cv2
video_capture = cv2.VideoCapture(1)
obama_img = face_recognition.load_image_file("obama.jpg")
obama_face_encoding = face_recognition.face_encodings(obama_img)[0]
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
while True:
ret, frame = video_capture.read()
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
if process_this_frame:
face_locations = face_recognition.face_locations(small_frame)
face_encodings = face_recognition.face_encodings(small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
match = face_recognition.compare_faces([obama_face_encoding], face_encoding)
if match[0]:
name = "Barack"
else:
name = "unknown"
face_names.append(name)
process_this_frame = not process_this_frame
for (top, right, bottom, left), name in zip(face_locations, face_names):
top *= 4
right *= 4
bottom *= 4
left *= 4
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), 2)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left+6, bottom-6), font, 1.0, (255, 255, 255), 1)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()5455
识别结果:
我直接在手机上网络了几张图试试,程序识别出了奥巴马。
这个库很cool啊!
3. python如何点击图片
# coding: utf-8
import cv2
import numpy as np
img = cv2.imread("E:\\workspace\\cvimg\\1.png")
#print img.shape
def on_EVENT_LBUTTONDOWN(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
xy = "%d,%d" % (x, y)
print xy
cv2.circle(img, (x, y), 1, (255, 0, 0), thickness = -1)
cv2.putText(img, xy, (x, y), cv2.FONT_HERSHEY_PLAIN,
1.0, (0,0,0), thickness = 1)
cv2.imshow("image", img)cv2.namedWindow("image")
cv2.setMouseCallback("image", on_EVENT_LBUTTONDOWN)
cv2.imshow("image", img)while(True):
try:
cv2.waitKey(100)
except Exception:
cv2.destroyWindow("image")
break
cv2.waitKey(0)
cv2.destroyAllWindow()
4. openCV针对python的训练器怎么做
此程序为先调用OpenCV自带的人脸检测模型,检测到人脸后,再调用我自己训练好的模型去识别人脸,使用时更改模型地址即可
#!usr/bin/env python
import cv2
font=cv2.FONT_HERSHEY_SIMPLEX
cascade1 = cv2.CascadeClassifier("D:\\opencv249\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt_tree.xml")
cascade2 = cv2.CascadeClassifier("D:\\opencv249\\opencv\\sources\\data\\haarcascades\\xml.xml")
cap = cv2.VideoCapture(0)
while True:
ret,frame = cap.read()
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
rect = cascade1.detectMultiScale(gray,scaleFactor=1.3,minNeighbors=9,minSize=(50,50),flags = cv2.cv.CV_HAAR_SCALE_IMAGE)
if not rect is ():
for x,y,z,w in rect:
roiImg = gray[y:y+w,x:x+z]
rect1 = cascade2.detectMultiScale(roiImg,scaleFactor=1.3,minNeighbors=9,minSize=(50,50),flags = cv2.cv.CV_HAAR_SCALE_IMAGE)
if not rect1 is ():
for (a,b,c,d) in rect1:
print "rect",rect1
cv2.putText(frame,'Chenym',(x,y), font, 2,(0,0,255),2)
cv2.rectangle(frame,(x+a,y+b),(x+a+c,y+b+d),(0,0,255),2)
cv2.imshow('frame',frame)
if cv2.waitKey(1) &0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
5. 基于python的自动驾驶交通牌指示灯识别程序代码
摘要 def __init__(self, **kwargs):
6. Python关于cv2的代码'int' object is not iterable. 我的环境是py3,这个报错不懂啊
Note
When using the FaceRecognizer interface in combination
with Python, please stick to Python 2. Some underlying scripts like
create_csv will not work in other versions, like Python 3.
cv2如果用上了人脸识别,最好用python2,不然一些基础脚本用不了。。。,
来源见 http://docs.opencv.org/2.4/moles/contrib/doc/facerec/facerec_api.html