Skip to content

Commit 9b896d8

Browse files
committed
python_opencv
1 parent 9f35a49 commit 9b896d8

9 files changed

Lines changed: 108 additions & 0 deletions

File tree

-12 KB
Binary file not shown.

python_opencv/1.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#coding:utf-8
2+
import cv2
3+
4+
capture=cv2.VideoCapture(0)
5+
#将capture保存为motion-jpeg,cv_fourcc为保存格式
6+
size = (int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),
7+
int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))
8+
#cv_fourcc值要设置对,不然无法写入,而且不报错,坑
9+
video=cv2.VideoWriter("VideoTest.avi", cv2.cv.CV_FOURCC('I','4','2','0'), 30, size)
10+
#isopened可以查看摄像头是否开启
11+
print capture.isOpened()
12+
num=0
13+
#要不断读取image需要设置一个循环
14+
while True:
15+
ret,img=capture.read()
16+
#视频中的图片一张张写入
17+
video.write(img)
18+
cv2.imshow('Video',img)
19+
key=cv2.waitKey(1)#里面数字为delay时间,如果大于0为刷新时间,
20+
#超过指定时间则返回-1,等于0没有返回值,但也可以读取键盘数值,
21+
cv2.imwrite('%s.jpg'%(str(num)),img)
22+
num=num+1
23+
if key==ord('q'):#ord为键盘输入对应的整数,
24+
break
25+
video.release()

python_opencv/2.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#coding=utf-8
2+
import os
3+
from PIL import Image, ImageDraw
4+
import cv
5+
6+
def detect_object(image):
7+
'''检测图片,获取人脸在图片中的坐标'''
8+
grayscale = cv.CreateImage((image.width, image.height), 8, 1)
9+
cv.CvtColor(image, grayscale, cv.CV_BGR2GRAY)
10+
11+
cascade = cv.Load("/usr/share/opencv/haarcascades/haarcascade_frontalface_alt_tree.xml")
12+
rect = cv.HaarDetectObjects(grayscale, cascade, cv.CreateMemStorage(), 1.1, 2,
13+
cv.CV_HAAR_DO_CANNY_PRUNING, (20,20))
14+
15+
result = []
16+
for r in rect:
17+
result.append((r[0][0], r[0][1], r[0][0]+r[0][2], r[0][1]+r[0][3]))
18+
19+
return result
20+
21+
def process(infile):
22+
'''在原图上框出头像并且截取每个头像到单独文件夹'''
23+
image = cv.LoadImage(infile);
24+
if image:
25+
faces = detect_object(image)
26+
27+
im = Image.open(infile)
28+
path = os.path.abspath(infile)
29+
save_path = os.path.splitext(path)[0]+"_face"
30+
try:
31+
os.mkdir(save_path)
32+
except:
33+
pass
34+
if faces:
35+
draw = ImageDraw.Draw(im)
36+
count = 0
37+
for f in faces:
38+
count += 1
39+
draw.rectangle(f, outline=(255, 0, 0))
40+
a = im.crop(f)
41+
file_name = os.path.join(save_path,str(count)+".jpg")
42+
# print file_name
43+
a.save(file_name)
44+
45+
drow_save_path = os.path.join(save_path,"out.jpg")
46+
im.save(drow_save_path, "JPEG", quality=80)
47+
else:
48+
print "Error: cannot detect faces on %s" % infile
49+
50+
if __name__ == "__main__":
51+
process("psb.jpg")

python_opencv/2.py~

Whitespace-only changes.

python_opencv/psb.jpg

90 KB
Loading

python_opencv/psb_face/1.jpg

8.73 KB
Loading

python_opencv/psb_face/out.jpg

86.1 KB
Loading

python_opencv/test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
mport cv2
2+
3+
4+
capture=cv2.VideoCapture(0)
5+
#将capture保存为motion-jpeg,cv_fourcc为保存格式
6+
size = (int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),
7+
int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))
8+
#cv_fourcc值要设置对,不然无法写入,而且不报错,坑
9+
video=cv2.VideoWriter("VideoTest.avi", cv2.cv.CV_FOURCC('I','4','2','0'), 30, size)
10+
#isopened可以查看摄像头是否开启
11+
print capture.isOpened()
12+
num=0
13+
#要不断读取image需要设置一个循环
14+
while True:
15+
ret,img=capture.read()
16+
#视频中的图片一张张写入
17+
video.write(img)
18+
cv2.imshow('Video',img)
19+
key=cv2.waitKey(1)#里面数字为delay时间,如果大于0为刷新时间,
20+
#超过指定时间则返回-1,等于0没有返回值,但也可以读取键盘数值,
21+
cv2.imwrite('%s.jpg'%(str(num)),img)
22+
num=num+1
23+
if key==ord('q'):#ord为键盘输入对应的整数,
24+
break
25+
video.release()import cv2
26+
img = cv2.imread("psd.jpg")
27+
28+
cv2.namedWindow("Image")
29+
cv2.imshow("image",img)
30+
31+
cv.waitKey(0)
32+

python_opencv/无标题文档~

Whitespace-only changes.

0 commit comments

Comments
 (0)