-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
72 lines (60 loc) · 2.28 KB
/
app.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
from flask import Flask, render_template
import cv2
import pickle
import cvzone
import numpy as np
app = Flask(__name__)
@app.route('/')
def project():
return render_template('index.html')
@app.route('/hero')
def home():
return render_template('index.html')
@app.route('/model')
def login():
return render_template('model.html')
@app.route('/predict_live')
def liv_pred():
# Video feed
cap = cv2.VideoCapture('carParkingInput.mp4')
with open('parkingSlotPosition', 'rb') as f:
posList = pickle.load(f)
width, height = 107, 48
def checkParkingSpace(imgPro):
spaceCounter = 0
for pos in posList:
x, y = pos
imgCrop = imgPro[y:y + height, x:x + width]
# cv2.imshow(str(x * y), imgCrop)
count = cv2.countNonZero(imgCrop)
if count < 900:
color = (0, 255, 0)
thickness = 5
spaceCounter += 1
else:
color = (0, 0, 255)
thickness = 2
cv2.rectangle(img, pos, (pos[0] + width, pos[1] + height), color, thickness)
"""cvzone.putTextRect(img, str(count), (x, y + height - 3), scale=1,
thickness=2, offset=0, colorR=color)"""
cvzone.putTextRect(img, f'Free: {spaceCounter}/{len(posList)}',(100, 50), scale=3,
thickness=5, offset=20, colorR=(200, 0, 0))
while True:
if cap.get(cv2.CAP_PROP_POS_FRAMES) == cap.get(cv2.CAP_PROP_FRAME_COUNT):
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
success, img = cap.read()
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imgBlur = cv2.GaussianBlur(imgGray, (3, 3), 1)
imgThreshold = cv2.adaptiveThreshold(imgBlur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY_INV, 25, 16)
imgMedian = cv2.medianBlur(imgThreshold, 5)
kernel = np.ones((3, 3), np.uint8)
imgDilate = cv2.dilate(imgMedian, kernel, iterations=1)
checkParkingSpace(imgDilate)
cv2.imshow("Image", img)
# cv2.imshow("ImageBlur", imgBlur)
# cv2.imshow("ImageThres", imgMedian)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if __name__ == '__main__':
app.run(debug=True)