forked from suyashkumar/seven-segment-ocr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseven_segment_ocr.py
64 lines (56 loc) · 2.39 KB
/
seven_segment_ocr.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
"""
seven-segment-ocr.py
Extracts numbers from a video of a seven segment display and
saves them to an output file.
@author: Suyash Kumar <suyashkumar2003@gmail.com>
"""
import argparse
import cv2
import image_selection
import digit_reader
"""
Reads selected digits at intervals specified by samplePeriod from.
the video specified by videoPath. Returns a list containing lists
of numbers read at each period.
"""
def read_video_digits(videoPath, samplePeriod):
video = cv2.VideoCapture(videoPath) # Open video
success,image = video.read() # Get first frame
selections=image_selection.getSelectionsFromImage(image)
fps = video.get(cv2.CAP_PROP_FPS) # Get FPS
frameInterval = int(round(float(fps) * float(samplePeriod)))
totalFrames = video.get(cv2.CAP_PROP_FRAME_COUNT)
digitMatrix = [] # Holds list of dgits from each sample
for frameNumber in xrange(0,int(round(totalFrames/frameInterval))):
video.set(cv2.CAP_PROP_POS_FRAMES, frameNumber*frameInterval) # Set frame to read next
success,image = video.read() # Get frame
digits = digit_reader.read_digits(image, selections) # Get digits
digitMatrix.append(digits)
video.release()
return digitMatrix
def to_csv(digitsReadArray, outputFileName):
with open(outputFileName,'w') as f:
for sample in digitsReadArray:
f.write(str(sample[0])+"."+str(sample[1])+str(sample[2]))
f.write("\n")
def to_file(digitsReadArray, outputFileName):
with open(outputFileName,'w') as f:
for sample in digitsReadArray:
for digit in sample:
f.write(str(digit))
f.write("\n")
if __name__=="__main__":
# Set up argument parsing:
parser = argparse.ArgumentParser()
parser.add_argument('--video', help = "Input Video File")
parser.add_argument('--output', help = "Output data file", default="out.csv")
parser.add_argument('--config', help = "How to format the digit output file.", default="none")
parser.add_argument('--period', help = "Period (in seconds) to sample the video at", default=1)
args = parser.parse_args()
digitsReadArray = read_video_digits(args.video, args.period)
if (args.config == "drok"):
# Output digit data in the A.BC drok power meter configuration
to_csv(digitsReadArray, args.output)
else:
# Output digit data to file
to_file(digitsReadArray, args.output)