This repository has been archived by the owner on Feb 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count_objects.py
102 lines (93 loc) · 3.99 KB
/
count_objects.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
#----This script creates/appends a more readable version of the file "detections.csv"----#
#!/usr/bin/env python
# coding: utf-8
# import packages
import os
import pandas as pd
import csv
from datetime import datetime
# read csv file with image detections from pretrained model
df = pd.read_csv('./output_folder/detections.csv')
# group by image and type of object and perform counts
counts = df.groupby(['image_id', 'object']).count().score
# filter rows with the objects of interest
counts = counts[counts.index.get_level_values(1).isin(['bicycle', 'car', 'person'])]
#put all the images in a list object
images = counts.index.get_level_values(0).unique()
# create or append a csv file with counts per object of interest
timestamp = datetime.now() #Return the current local date and time
if os.path.isfile('./output_folder/report.csv'):
with open('./output_folder/report.csv', 'a') as file:
writer = csv.writer(file, delimiter=",", lineterminator="\n")
car = 0
person = 0
bicycle = 0
motorcycle = 0
bus = 0
truck = 0
for i in range(len(images)):
for j in range(len(counts)):
if images[i] == counts.index[j][0] and counts.index[j][1] == 'car':
car = counts[j]
continue
if images[i] == counts.index[j][0] and counts.index[j][1] == 'person':
person = counts[j]
continue
if images[i] == counts.index[j][0] and counts.index[j][1] == 'bicycle':
bicycle = counts[j]
continue
if images[i] == counts.index[j][0] and counts.index[j][1] == 'motorcycle':
motorcycle = counts[j]
continue
if images[i] == counts.index[j][0] and counts.index[j][1] == 'bus':
bus = counts[j]
continue
if images[i] == counts.index[j][0] and counts.index[j][1] == 'truck':
truck = counts[j]
# print(timestamp, images[i], car, person, bicycle, motorcycle, bus, truck, sep=",")
row = [timestamp, images[i], car, person, bicycle, motorcycle, bus, truck]
writer.writerow(row)
car = 0
person = 0
bicycle = 0
motorcycle = 0
bus = 0
truck = 0
else:
with open('./output_folder/report.csv', 'a') as file:
file.write('timestamp,image,car,person,bicycle,motorcycle,bus,truck\n')
writer = csv.writer(file, delimiter=",", lineterminator="\n")
car = 0
person = 0
bicycle = 0
motorcycle = 0
bus = 0
truck = 0
for i in range(len(images)):
for j in range(len(counts)):
if images[i] == counts.index[j][0] and counts.index[j][1] == 'car':
car = counts[j]
continue
if images[i] == counts.index[j][0] and counts.index[j][1] == 'person':
person = counts[j]
continue
if images[i] == counts.index[j][0] and counts.index[j][1] == 'bicycle':
bicycle = counts[j]
continue
if images[i] == counts.index[j][0] and counts.index[j][1] == 'motorcycle':
motorcycle = counts[j]
continue
if images[i] == counts.index[j][0] and counts.index[j][1] == 'bus':
bus = counts[j]
continue
if images[i] == counts.index[j][0] and counts.index[j][1] == 'truck':
truck = counts[j]
# print(timestamp, images[i], car, person, bicycle, motorcycle, bus, truck, sep=",")
row = [timestamp, images[i], car, person, bicycle, motorcycle, bus, truck]
writer.writerow(row)
car = 0
person = 0
bicycle = 0
motorcycle = 0
bus = 0
truck = 0