forked from JunhaoGao/detect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetData.py
More file actions
68 lines (64 loc) · 2.95 KB
/
Copy pathgetData.py
File metadata and controls
68 lines (64 loc) · 2.95 KB
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
import cv2
import os
import xml.dom.minidom as xdm
videoPath = "D:\\detectProject\\data\\first" #input your video path
for root, dirs, files in os.walk(videoPath):
for videoFile in files:
videoCapture = cv2.VideoCapture(os.path.join(root, videoFile))
fps = videoCapture.get(cv2.cv.CV_CAP_PROP_FPS)
size = (int(videoCapture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),
int(videoCapture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))
print os.path.join(root, videoFile)
success, frame = videoCapture.read()
#frame = cv2.resize(frame, (480, 320)) #zip the frame
c = 0
while success:
if (c % 5 == 0):
#frame = cv2.resize(frame, (480, 320)) #zip the frame
cv2.imwrite('D:/detectProject/dataframe/' + 'image' + str(videoFile).split('.MOV')[0] + str(c) + '.jpg',
frame)
success, frame = videoCapture.read()
c = c + 1
xmlFolderPath = "D:\\detectProject\\datamark\\xml\\first"
for root, dirs, files in os.walk(xmlFolderPath):
for xmlFile in files:
xmlPath = os.path.join(root, xmlFile)
dom = xdm.parse(xmlPath)
rootXML = dom.documentElement
imagePath = rootXML.getElementsByTagName('path')[0].firstChild.data
imageName = imagePath.split('\\')[-1]
imageNameArray = str(imageName).split('.MOV')
allObject = rootXML.getElementsByTagName('object')
imgPath = "D:\\detectProject\\dataframe\\first\\" + imageNameArray[0] + imageNameArray[1]
img = cv2.imread(imgPath, 3)
i = 0
for myObject in allObject:
i = i + 1
bndBox = myObject.getElementsByTagName('bndbox')[0]
xmin = int(bndBox.getElementsByTagName('xmin')[0].firstChild.data)
ymin = int(bndBox.getElementsByTagName('ymin')[0].firstChild.data)
xmax = int(bndBox.getElementsByTagName('xmax')[0].firstChild.data)
ymax = int(bndBox.getElementsByTagName('ymax')[0].firstChild.data)
xavg = (xmin + xmax)/2
yavg = (ymin + ymax)/2
if (ymax - ymin) <= 100:
y1 = int(yavg - 50)
y2 = int(yavg + 50)
x1 = int(xavg - 25)
x2 = int(xavg + 25)
size = 'S'
elif 100 < (ymax - ymin) <= 200:
y1 = int(yavg - 100)
y2 = int(yavg + 100)
x1 = int(xavg - 50)
x2 = int(xavg + 50)
size = 'M'
elif (ymax - ymin) > 200:
y1 = int(yavg - 200)
y2 = int(yavg + 200)
x1 = int(xavg - 100)
x2 = int(xavg + 100)
size = 'L'
img2 = img[y1:y2, x1:x2, :]
cv2.imwrite("D:\\detectProject\\traindata\\first" + str(i) + 'of' + size + imageName, img2)
print 'success once' + str(xmlFile)