吴裕雄 实战PYTHON编程

Posted 天生自然

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了吴裕雄 实战PYTHON编程相关的知识,希望对你有一定的参考价值。

import cv2

cv2.namedWindow("ShowImage1")
cv2.namedWindow("ShowImage2")
image1 = cv2.imread("F:\pythonBasepythonex\ch10\media\img01.jpg")
#image1 = cv2.imread("media\img01.jpg", 1)
image2 = cv2.imread("F:\pythonBasepythonex\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:\pythonBasepythonex\ch10\media\img01.jpg", 0)
cv2.imshow("ShowImage", image)
cv2.imwrite("F:\pythonBasepythonex\ch10\img01copy1.jpg", image)
cv2.imwrite("F:\pythonBasepythonex\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:\pythonBasepythonex\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:\pythonBasepythonex\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\Libraryetc\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:\pythonBasepythonex\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\Libraryetc\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:\pythonBasepythonex\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\Libraryetc\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:\pythonBasepythonex\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:\pythonBasepythonex\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\Libraryetc\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:\pythonBasepythonex\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:\pythonBasepythonex\" + 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\Libraryetc\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:\pythonBasepythonex\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编程的主要内容,如果未能解决你的问题,请参考以下文章

吴裕雄 实战python编程

吴裕雄 实战PYTHON编程

吴裕雄--python编程:CGI编程

吴裕雄--天生自然python编程:turtle模块绘图

吴裕雄--天生自然python编程:pycharm常用快捷键问题

吴裕雄--天生自然python编程:正则表达式