码迷,mamicode.com
首页 > 编程语言 > 详细

吴裕雄 实战PYTHON编程(9)

时间:2018-12-03 23:03:23      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:find   one   resize   file   ssi   opencv3   pil   wait   imshow   

import cv2

cv2.namedWindow("ShowImage1")
cv2.namedWindow("ShowImage2")
image1 = cv2.imread("F:\\pythonBase\pythonex\\ch10\\media\\img01.jpg")
#image1 = cv2.imread("media\\img01.jpg", 1)
image2 = cv2.imread("F:\\pythonBase\pythonex\\ch10\\media\\img01.jpg", 0)
cv2.imshow("ShowImage1", image1)
cv2.imshow("ShowImage2", image2)
# cv2.waitKey()
cv2.waitKey(10000)
cv2.destroyAllWindows()

import cv2

cv2.namedWindow("ShowImage")
image = cv2.imread("F:\\pythonBase\pythonex\\ch10\\media\\img01.jpg", 0)
cv2.imshow("ShowImage", image)
cv2.imwrite("F:\\pythonBase\pythonex\\ch10\\img01copy1.jpg", image)
cv2.imwrite("F:\\pythonBase\pythonex\\ch10\\img01copy2.jpg", image, [int(cv2.IMWRITE_JPEG_QUALITY), 50])
cv2.waitKey(10000)
cv2.destroyWindow("ShowImage")

import cv2, numpy

cv2.namedWindow("plot")
image = cv2.imread("F:\\pythonBase\pythonex\\ch10\\media\\background.jpg")
cv2.line(image, (50,50), (300,300), (255,0,0), 2)
cv2.rectangle(image, (500,20), (580,100), (0,255,0), 3)
cv2.rectangle(image, (100,300), (150,360), (0,0,255), -1)
cv2.circle(image, (500,300), 40, (255,255,0), -1)
pts = numpy.array([[300,300],[300,340],[350,320]], numpy.int32)
cv2.polylines(image, [pts], True, (0,255,255), 2)
cv2.putText(image,"background.jpg", (350,420), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,255,255), 2)
cv2.imshow("plot", image)
cv2.waitKey(5000)
cv2.destroyAllWindows()

import cv2

casc_path = "C:\\Users\\acer\\Anaconda3\\envs\\python36\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(casc_path)
imagename = cv2.imread("F:\\pythonBase\pythonex\\ch10\\media\\person1.jpg")
faces = faceCascade.detectMultiScale(imagename, scaleFactor=1.1, minNeighbors=5, minSize=(30,30), flags = cv2.CASCADE_SCALE_IMAGE)
#imagename.shape[0]:图片高度,imagename.shape[1]:图片宽度
cv2.rectangle(imagename, (10,imagename.shape[0]-20), (110,imagename.shape[0]), (0,0,0), -1)
cv2.putText(imagename,"Find " + str(len(faces)) + " face!", (10,imagename.shape[0]-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2)
for (x,y,w,h) in faces:
cv2.rectangle(imagename,(x,y),(x+w, y+h),(128,255,0),2)
cv2.namedWindow("facedetect")
cv2.imshow("facedetect", imagename)
cv2.waitKey(0)
cv2.destroyWindow("facedetect")

import cv2

# casc_path = "C:\\ProgramData\\Anaconda3\\pkgs\\opencv3-3.1.0-py27_0\\Library\etc\\haarcascades\\haarcascade_frontalface_default.xml"
casc_path = "C:\\Users\\acer\\Anaconda3\\envs\\python36\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(casc_path)
image = cv2.imread("F:\\pythonBase\pythonex\\ch10\\media\\person3.jpg")
faces = faceCascade.detectMultiScale(image, scaleFactor=1.1, minNeighbors=5, minSize=(30,30), flags = cv2.CASCADE_SCALE_IMAGE)
#image.shape[0]:取得图片高度,image.shape[1]:取得图片宽度
cv2.rectangle(image, (10,image.shape[0]-20), (110,image.shape[0]), (0,0,0), -1)
cv2.putText(image,"Find " + str(len(faces)) + " face!", (10,image.shape[0]-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2)
for (x,y,w,h) in faces:
cv2.rectangle(image,(x,y),(x+w, y+h),(128,255,0),2)
cv2.namedWindow("facedetect")
cv2.imshow("facedetect", image)
cv2.waitKey(10000)
cv2.destroyWindow("facedetect")

import cv2

# casc_path = "C:\\ProgramData\\Anaconda3\\pkgs\\opencv3-3.1.0-py27_0\\Library\etc\\haarcascades\\haarcascade_frontalface_default.xml"
casc_path = "C:\\Users\\acer\\Anaconda3\\envs\\python36\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(casc_path)
image = cv2.imread("F:\\pythonBase\pythonex\\ch10\\media\\person8.jpg")
faces = faceCascade.detectMultiScale(image, scaleFactor=1.1, minNeighbors=5, minSize=(30,30), flags = cv2.CASCADE_SCALE_IMAGE)
#image.shape[0]:获取图片高度,image.shape[1]:获取图片宽度
cv2.rectangle(image, (10,image.shape[0]-20), (110,image.shape[0]), (0,0,0), -1)
cv2.putText(image,"Find " + str(len(faces)) + " face!", (10,image.shape[0]-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2)
for (x,y,w,h) in faces:
cv2.rectangle(image,(x,y),(x+w, y+h),(128,255,0),2)
cv2.namedWindow("facedetect")
cv2.imshow("facedetect", image)
cv2.waitKey(10000)
cv2.destroyWindow("facedetect")

import cv2

from PIL import Image

# casc_path = "C:\\ProgramData\\Anaconda3\\pkgs\\opencv3-3.1.0-py27_0\\Library\etc\\haarcascades\\haarcascade_frontalface_default.xml"
casc_path = "C:\\Users\\acer\\Anaconda3\\envs\\python36\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(casc_path)
imagename = "F:\\pythonBase\pythonex\\ch10\\media\\person1.jpg"
image = cv2.imread(imagename)
faces = faceCascade.detectMultiScale(image, scaleFactor=1.1, minNeighbors=5, minSize=(30,30), flags = cv2.CASCADE_SCALE_IMAGE)
count = 1
for (x,y,w,h) in faces:
cv2.rectangle(image, (x,y), (x+w,y+h), (128,255,0), 2)
filename = "F:\\pythonBase\pythonex\\ch10\\" + str(count)+ ".jpg"
image1 = Image.open(imagename)
image2 = image1.crop((x, y, x+w, y+h))
image3 = image2.resize((200, 200), Image.ANTIALIAS)
image3.save(filename)
count += 1
cv2.namedWindow("facedetect")
cv2.imshow("facedetect", image)
cv2.waitKey(10000)
cv2.destroyWindow("facedetect")

import cv2
from PIL import Image

# casc_path = "C:\\ProgramData\\Anaconda3\\pkgs\\opencv3-3.1.0-py27_0\\Library\etc\\haarcascades\\haarcascade_frontalface_default.xml"
casc_path = "C:\\Users\\acer\\Anaconda3\\envs\\python36\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(casc_path)
imagename = "F:\\pythonBase\pythonex\\ch10\\media\\person3.jpg"
image = cv2.imread(imagename)
faces = faceCascade.detectMultiScale(image, scaleFactor=1.1, minNeighbors=5, minSize=(30,30), flags = cv2.CASCADE_SCALE_IMAGE)
count = 1
for (x,y,w,h) in faces:
cv2.rectangle(image, (x,y), (x+w,y+h), (128,255,0), 2)
filename = "F:\\pythonBase\pythonex\\" + str(count)+ ".jpg"
image1 = Image.open(imagename)
image2 = image1.crop((x, y, x+w, y+h))
image3 = image2.resize((200, 200), Image.ANTIALIAS)
image3.save(filename)
count += 1
cv2.namedWindow("facedetect")
cv2.imshow("facedetect", image)
cv2.waitKey(10000)
cv2.destroyWindow("facedetect")

import cv2
from PIL import Image

# casc_path = "C:\\ProgramData\\Anaconda3\\pkgs\\opencv3-3.1.0-py27_0\\Library\etc\\haarcascades\\haarcascade_frontalface_default.xml"
casc_path = "C:\\Users\\acer\\Anaconda3\\envs\\python36\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(casc_path)
imagename = "F:\\pythonBase\pythonex\\ch10\\media\\person8.jpg"
image = cv2.imread(imagename)
faces = faceCascade.detectMultiScale(image, scaleFactor=1.1, minNeighbors=5, minSize=(30,30), flags = cv2.CASCADE_SCALE_IMAGE)
count = 1
for (x,y,w,h) in faces:
cv2.rectangle(image, (x,y), (x+w,y+h), (128,255,0), 2)
filename = "F:\\pythonBase\\pythonex\\" + str(count)+ ".jpg"
image1 = Image.open(imagename)
image2 = image1.crop((x, y, x+w, y+h))
image3 = image2.resize((200, 200), Image.ANTIALIAS)
image3.save(filename)
count += 1
cv2.namedWindow("facedetect")
cv2.imshow("facedetect", image)
cv2.waitKey(10000)
cv2.destroyWindow("facedetect")

吴裕雄 实战PYTHON编程(9)

标签:find   one   resize   file   ssi   opencv3   pil   wait   imshow   

原文地址:https://www.cnblogs.com/tszr/p/10061382.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!