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)的更多相关文章

  1. 吴裕雄 实战PYTHON编程(10)

    import cv2 cv2.namedWindow("frame")cap = cv2.VideoCapture(0)while(cap.isOpened()): ret, im ...

  2. 吴裕雄 实战PYTHON编程(8)

    import pandas as pd df = pd.DataFrame( {"林大明":[65,92,78,83,70], "陈聪明":[90,72,76, ...

  3. 吴裕雄 实战PYTHON编程(7)

    import os from win32com import client word = client.gencache.EnsureDispatch('Word.Application')word. ...

  4. 吴裕雄 实战PYTHON编程(6)

    import matplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['Simhei']plt.rcParams['axes.unicode ...

  5. 吴裕雄 实战PYTHON编程(5)

    text = '中华'print(type(text))#<class 'str'>text1 = text.encode('gbk')print(type(text1))#<cla ...

  6. 吴裕雄 实战PYTHON编程(4)

    import hashlib md5 = hashlib.md5()md5.update(b'Test String')print(md5.hexdigest()) import hashlib md ...

  7. 吴裕雄 实战python编程(3)

    import requests from bs4 import BeautifulSoup url = 'http://www.baidu.com'html = requests.get(url)sp ...

  8. 吴裕雄 实战python编程(2)

    from urllib.parse import urlparse url = 'http://www.pm25x.com/city/beijing.htm'o = urlparse(url)prin ...

  9. 吴裕雄 实战python编程(1)

    import sqlite3 conn = sqlite3.connect('E:\\test.sqlite') # 建立数据库联接cursor = conn.cursor() # 建立 cursor ...

随机推荐

  1. C# DateTime 月第一天和最后一天 取法

    取得某月和上个月第一天和最后一天的方法 /// <summary> /// 取得某月的第一天 /// </summary> /// <param name="d ...

  2. Linq快速入门——扩展方法

    Linq为我们提供了许多扩展方法,方便我们对数据源进行操作(Where,Select...).即使你不了解算法,也能使用Linq当回牛人.扩展方法本质并不是什么高深的技术,说白了就是一个Static静 ...

  3. linux 守护进程(daemon process)代码-详细注释

    1. 进程组 组长不能创建新的 会话. 其它进程可以创建新的会话,创建后既成为会话首领,同时失去控制终端. 2. 会话首领可以重新打开控制终端 1 #include <stdio.h> 2 ...

  4. jquery dataTable的学习

    http://www.cnblogs.com/nier/archive/2012/03/19/2406450.html 分页 bPaginite:true;是否启用分页功能 sPaginationTy ...

  5. TraceView 使用详解 android eclipse

    先看命令 (配置好环境变量的情况下,直接traceview+空格+ trace文件路径即可): TraceView是什么 Traceview是android平台配备一个很好的性能分析的工具.它可以通过 ...

  6. phonegap 2.9 IOS Xcode 搭建环境

    一:下载phoneGap2.9和安装Xcode5(目前最新版) 选择2.9是因为3.0以上坑爹版本编译神马的要在有网络情况. 二: 下载phonegap后解压到你的指定文件夹中,解压后找到create ...

  7. 【转载】webstorm-前端javascript开发神器中文教程和技巧分享

    webstorm是一款前端javascript开发编辑的神器,此文介绍webstorm的中文教程和技巧分享. webstorm8.0.3中文汉化版下载:百度网盘下载:http://pan.baidu. ...

  8. [UE4]FString常用API

    转自:http://aigo.iteye.com/blog/2279808 将int或float转换为string: 将FString转换为char*: 将string转换为int或者float: 字 ...

  9. UVA133

    减少领取救济金排队的长度是一个严重的问题,The New National Green Labour RhinocerosParty (这个党派)依据如下规则.每天来领取救济金的人排成一个大圆环.任选 ...

  10. windows巡检

    参考网站:   http://www.jb51.net/os/windows/525017.html 系统自带工具巡检  : 先说说如何检查系统健康度的方法,Win+R只有只要输入一个命令: perf ...