-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_get_coord.py
76 lines (59 loc) · 2.16 KB
/
auto_get_coord.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
# -*- coding: utf-8 -*-
"""
Created on Tue Dec 27 09:32:02 2016
@author: http://blog.csdn.net/lql0716
"""
import cv2
import numpy as np
current_pos = None
tl = None
br = None
#鼠标事件
def get_rect(im, title='get_rect'): # (a,b) = get_rect(im, title='get_rect')
mouse_params = {'tl': None, 'br': None, 'current_pos': None,
'released_once': False}
cv2.namedWindow(title)
cv2.moveWindow(title, 100, 100)
def onMouse(event, x, y, flags, param):
param['current_pos'] = (x, y)
if param['tl'] is not None and not (flags & cv2.EVENT_FLAG_LBUTTON):
param['released_once'] = True
if flags & cv2.EVENT_FLAG_LBUTTON:
if param['tl'] is None:
param['tl'] = param['current_pos']
elif param['released_once']:
param['br'] = param['current_pos']
cv2.setMouseCallback(title, onMouse, mouse_params)
cv2.imshow(title, im)
while mouse_params['br'] is None:
im_draw = np.copy(im)
if mouse_params['tl'] is not None:
cv2.rectangle(im_draw, mouse_params['tl'],
mouse_params['current_pos'], (255, 0, 0))
cv2.imshow(title, im_draw)
_ = cv2.waitKey(10)
cv2.destroyWindow(title)
tl = (min(mouse_params['tl'][0], mouse_params['br'][0]),
min(mouse_params['tl'][1], mouse_params['br'][1]))
br = (max(mouse_params['tl'][0], mouse_params['br'][0]),
max(mouse_params['tl'][1], mouse_params['br'][1]))
return (tl, br) #tl=(y1,x1), br=(y2,x2)
#读取摄像头/视频,然后用鼠标事件画框
def readImg(pathName):
im=cv2.imread(pathName)
h,w=im.shape[:2]
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
(a,b) = get_rect(im, title='get_rect') #鼠标画矩形框
res=[]
res.append()
if __name__ == '__main__':
img_path="/home/data4/zhangchunyan/DriverLicense/data/versions_92/"
files = os.listdir(img_path)
for file in files:
name,_=os.path.splitext(file)
out_txt=open(name+".txt",'w')
lists = readImg(img_path+file)
for line in lists:
out_txt.write(line)
out_txt.write('\n')
out_txt.close()