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