-
Notifications
You must be signed in to change notification settings - Fork 0
/
cv.py
115 lines (95 loc) · 3.34 KB
/
cv.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
#!/usr/bin/python3
#
# Copyright (c) 2020, NVIDIA CORPORATION. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
from max7219 import max
import sys
from time import strftime
import jetson.utils
import jetson.inference
import sevenSeg
import database
VEHICLES = {3: "car", 8: "truck", }
# Record to MP4
RECORD = False
# Testing with small 7seg
TEST = True
if TEST:
miniseg = max()
# initialize camera and NN settings
videoIn = "csi://0"
videoInArgs = ["--input-flip=rotate-180"]
date = strftime("%m%d-%H%M%S")
if RECORD:
videoOut = f"./video/video-{date}.mp4"
else:
videoOut = ""
neuralnet = "ssd-mobilenet-v2"
overlay = "box,labels,conf"
threshold = 0.5
# init database
db = database.Database()
GARAGE = "Schrank"
count = db.getCurrentCount(GARAGE)
# init 7seg
seg = sevenSeg.sevenseg()
if TEST:
miniseg.write(count)
else:
seg.updateDisplay(count)
print(f'currently {count} cars in garage, updating display')
# load the object detection network
net = jetson.inference.detectNet(neuralnet, threshold=threshold)
# create video sources & outputs
input = jetson.utils.videoSource(videoIn, videoInArgs)
output = jetson.utils.videoOutput(videoOut)
# process frames until the user exits
while True:
if TEST:
miniseg.write(count)
else:
seg.updateDisplay(count)
# capture the next image
img = input.Capture()
# detect objects in the image (with overlay)
detections = net.Detect(img, overlay=overlay)
# print the detections
print("detected {:d} objects in image".format(len(detections)))
for detection in detections:
print(detection)
if detection.ClassID in VEHICLES.keys():
count = db.updateDatabase(GARAGE)
if TEST:
miniseg.write(count)
else:
seg.updateDisplay(count)
print(
f'{VEHICLES[detection.ClassID]} detected, count is now {count}')
# render the image
output.Render(img)
# update the title bar
output.SetStatus("{:s} | Network {:.0f} FPS".format(
neuralnet, net.GetNetworkFPS()))
# print out performance info
net.PrintProfilerTimes()
# exit on input/output EOS
if not input.IsStreaming() or not output.IsStreaming():
break