-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
120 lines (93 loc) · 3.92 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# encoding: utf-8
import os
import time
from argparse import ArgumentParser
from datetime import datetime
from logging import getLogger, DEBUG, basicConfig
import cv2
import numpy as np
import torch
import torch.nn.functional as F
from torch.autograd import Variable
USE_CUDA = torch.cuda.is_available()
# ======================================================================================================================
def detectFace(im):
start = time.time()
im = cv2.resize(im, (1024, 1024))
im_tensor = torch.from_numpy(im.transpose((2, 0, 1)))
im_tensor = im_tensor.float().div(255)
# loc, conf = net(Variable(torch.unsqueeze(im_tensor, 0).cuda() if USE_CUDA else torch.unsqueeze(im_tensor, 0)))
# boxes, labels, probs = data_encoder.decode(loc.data.squeeze(0).cpu() if USE_CUDA else loc.data.squeeze(0), F.softmax(conf.squeeze(0), 1).data.cpu() if USE_CUDA else F.softmax(conf.squeeze(0), 1).data)
# ps = []
# for p in probs:
# pitem = p.item() if torch.is_tensor(p) else p
# ps.append(pitem)
print('detectFace time:', time.time() - start)
# return boxes, ps
def detectFace_1(im):
start = time.time()
im = cv2.resize(im, (1024, 1024))
im_tensor = torch.from_numpy(im.transpose((2, 0, 1)))
im_tensor = Variable(torch.unsqueeze(im_tensor, 0).cuda() if USE_CUDA else torch.unsqueeze(im_tensor, 0))
im_tensor = im_tensor.float().div(255)
# loc, conf = net(Variable(torch.unsqueeze(im_tensor, 0).cuda() if USE_CUDA else torch.unsqueeze(im_tensor, 0)))
# boxes, labels, probs = data_encoder.decode(loc.data.squeeze(0).cpu() if USE_CUDA else loc.data.squeeze(0), F.softmax(conf.squeeze(0), 1).data.cpu() if USE_CUDA else F.softmax(conf.squeeze(0), 1).data)
# ps = []
# for p in probs:
# pitem = p.item() if torch.is_tensor(p) else p
# ps.append(pitem)
print('detectFace_1 time:', time.time() - start)
# return boxes, ps
def get_face_with_video():
cam = cv2.VideoCapture(0) # 调用计算机摄像头,一般默认为0
width = int(cam.get(cv2.CAP_PROP_FRAME_WIDTH) + 0.5)
height = int(cam.get(cv2.CAP_PROP_FRAME_HEIGHT) + 0.5)
fourcc = cv2.VideoWriter_fourcc(*'mp4v') # 定义编码
out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (width, height)) # 创建videowriter对象
while(cam.isOpened() is True):
ret, frame = cam.read() # 逐帧捕获
if ret is True:
# 输出当前帧
detectFace(frame)
detectFace_1(frame)
cv2.imshow('MyCamera', frame)
if (cv2.waitKey(1) & 0xFF) == ord('q'):
break
else:
break
out.release()
cam.release()
cv2.destroyAllWindow()
def test():
img = cv2.imread('550514.jpg')
img = cv2.resize(img, (1280, 720))
rows, cols, ch = img.shape
pts1 = np.float32([[50, 50], [200, 50], [50, 200]])
pts2 = np.float32([[50, 34], [200, 50], [50, 186]])
M = cv2.getAffineTransform(pts1, pts2)
dst = cv2.warpAffine(img, M, (cols, rows))
cv2.imshow('Input', img)
cv2.imshow('Output', dst)
cv2.imwrite('111.jpg', dst)
cv2.waitKey(0)
# plt.subplot(121), plt.imshow(img), plt.title('Input')
# plt.subplot(122), plt.imshow(dst), plt.title('Output')
# plt.show()
return
def test_1():
img = cv2.imread('568640.jpg')
rows, cols, ch = img.shape
pts1 = np.float32([[56, 65], [368, 52], [28, 387], [389, 390]])
pts2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])
M = cv2.getPerspectiveTransform(pts1, pts2)
dst = cv2.warpPerspective(img, M, (300, 300))
cv2.imshow('Input', img)
cv2.imshow('Output', dst)
cv2.waitKey(0)
# plt.subplot(121), plt.imshow(img), plt.title('Input')
# plt.subplot(122), plt.imshow(dst), plt.title('Output')
# plt.show()
return
if __name__ == '__main__':
# get_face_with_video()
test()