-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper_func.py
112 lines (94 loc) · 3.47 KB
/
helper_func.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
from datetime import datetime
from os import read
import cv2
import imutils
import numpy as np
import easyocr
import os
from tkinter import messagebox
import tkinter as tk
def capture_image_from_cam_into_temp():
cam = cv2.VideoCapture(0, cv2.CAP_DSHOW)
cv2.namedWindow("test")
# img_counter = 0
while True:
ret, frame = cam.read()
if not ret:
print("failed to grab frame")
break
cv2.imshow("test", frame)
k = cv2.waitKey(1)
if k % 256 == 27:
# ESC pressed
print("Escape hit, closing...")
break
elif k % 256 == 32:
# SPACE pressed
if not os.path.isdir('temp'):
os.mkdir('temp', mode=0o777) # make sure the directory exists
# img_name = "./temp/opencv_frame_{}.png".format(img_counter)
img_name = "./temp/test_img.png"
print('imwrite=', cv2.imwrite(filename=img_name, img=frame))
print("{} written!".format(img_name))
# img_counter += 1
cam.release()
cv2.destroyAllWindows()
return True
def captureFace(ent):
filename = os.getcwd()+'\\temp\\test_img.png'
# messagebox.showinfo(
# 'SUCCESS!!!', 'Press Space Bar to click picture and ESC to exit')
res = None
res = messagebox.askquestion(
'Click Picture', 'Press Space Bar to click picture and ESC to exit')
if res == 'yes':
capture_image_from_cam_into_temp()
ent.insert(tk.END, filename)
return True
def getNumberPlateSting(image):
img = cv2.imread(image)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
bfilter = cv2.bilateralFilter(gray, 11, 17, 17) # noise removal
edged = cv2.Canny(bfilter, 30, 200) # edge detection
keypoints = cv2.findContours(
edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = imutils.grab_contours(keypoints)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:10]
location = None
for contour in contours:
approx = cv2.approxPolyDP(contour, 10, True)
if(len(approx) == 4):
location = approx
break
mask = np.zeros(gray.shape, np.uint8)
new_image = cv2.drawContours(mask, [location], 0, 255, -1)
new_image = cv2.bitwise_and(img, img, mask=mask)
(x, y) = np.where(mask == 255)
(x1, y1) = (np.min(x), np.min(y))
(x2, y2) = (np.max(x), np.max(y))
cropped_image = gray[x1:x2+1, y1:y2+1]
reader = easyocr.Reader(['en'])
result = reader.readtext(cropped_image)
text = result[0][-2]
return text
# function accepts two timestamps and returns the hr difference
def find_hr_diff(start_time, end_time):
if(type(start_time) == str):
print('is_string')
duration = end_time - start_time
duration_in_s = duration.total_seconds()
hours = max(divmod(duration_in_s, 3600)[0], 1)
return hours
# function to accept start,end time and base charge and computes total charge
def calculate_charges(start_time, end_time, base_charge=10):
time_parked = find_hr_diff(start_time=start_time, end_time=end_time)
total_charges = time_parked*base_charge
return (time_parked, total_charges)
then = datetime(2012, 3, 5, 23, 8, 15)
now = datetime.now()
# print(calculate_charges(then, now))
# print('1', getNumberPlateSting('1.jpg'))
# print('2', getNumberPlateSting('2.jfif'))
# print('3', getNumberPlateSting('3.jfif'))
# print('4', getNumberPlateSting('4.jfif'))
# print('5', getNumberPlateSting('5.jpeg'))