-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcapture.py
153 lines (100 loc) · 4.47 KB
/
capture.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
import os
class Capture:
def Collect(num_samples):
global one,two,three,four,five,none
capture = cv.VideoCapture(0)
capture.set(cv.CAP_PROP_FRAME_WIDTH, 1024)
capture.set(cv.CAP_PROP_FRAME_HEIGHT, 768)
count = 0
switch = False
# This the ROI size, the size of images saved will be box_size -10
box_size = 234
# Getting the width of the frame from the camera properties
width = int(capture.get(3))
while True:
# Read frame by frame
ret, frame = capture.read()
# Flip the frame laterally
frame = cv.flip(frame, 1)
# Break the loop if there is trouble reading the frame.
if not ret:
break
# If counter is equal to the number samples then reset triger and the counter
if count == num_samples:
switch = not switch
count = 0
cv.rectangle(frame, (width - box_size, 0), (width, box_size), (0, 0, 0), 2)
cv.namedWindow("Collecting images", cv.WINDOW_NORMAL)
if switch == True:
ROI = frame[5: box_size-5 , width-box_size+5: width-5] # LFU
if class_name == "1":
one.append([ROI])
if class_name == "2":
two.append([ROI])
if class_name == "3":
three.append([ROI])
if class_name == "4":
four.append([ROI])
if class_name == "5":
five.append([ROI])
if class_name == "none":
none.append([ROI])
# Increment the counter
count += 1
# Text for the counter
text = "Collected Samples of {}: {}".format(class_name, count)
else:
text = "Press 1-0 and n, for collecting data-set."
# Show the counter on the imaege
cv.putText(frame, text, (3, 350), cv.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 0), 1, cv.LINE_AA)
# Display the window
cv.imshow("Collecting images", frame)
# Wait 1 ms
k = cv.waitKey(1)
if k == ord('1'):
switch = not switch
class_name = "1" # this is defined for eval(): This will represent the one[]
one = []
if k == ord('2'):
switch = not switch
class_name = "2"
two = []
if k == ord('3'):
switch = not switch
class_name = "3"
three = []
if k == ord('4'):
switch = not switch
class_name = "4"
four = []
if k == ord('5'):
switch = not switch
class_name = "5"
five = []
if k == ord('n'):
switch = not switch
class_name = "none"
none = []
if k == ord('q'):
break
# Release the camera and destroy the window
capture.release()
cv.destroyAllWindows()
data = [one,two,three,four,five,none]
# return all the lists containing our dataset!
return data
def Show(data):
# Set the figure size
plt.figure(figsize=[30,20])
# Set the rows and columns
rows, cols = 6,10
# Iterate for each class
for class_index, class_name in enumerate(data):
r = np.random.randint(10, size=8);
for i, example_index in enumerate(r,1):
plt.subplot(rows,cols,class_index*cols + i);
plt.imshow(class_name[example_index][0][:,:,::-1]); # converting BGR to RGB
plt.axis('off');