-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.py
144 lines (128 loc) · 3.29 KB
/
detect.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# %%
import logging
import os
from argparse import ArgumentParser
from datetime import datetime
from pathlib import Path
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from tqdm import tqdm
from ultralytics import YOLO
logging.getLogger("ultralytics").setLevel(logging.ERROR)
# %%
def batch(iterable, n=1):
l = len(iterable)
for ndx in range(0, l, n):
yield iterable[ndx : min(ndx + n, l)]
# %%
parser = ArgumentParser(description="detect.py")
parser.add_argument(
"--save_runs",
action="store_true",
default=False,
help="export processed images in files",
)
parser.add_argument(
"--force",
action="store_true",
default=False,
help="force to re-detect",
)
parser.add_argument(
"--batch_size",
type=int,
default=100,
help="batch size",
)
parser.add_argument(
"--data",
type=str,
default="images",
help="location of crawled images",
)
parser.add_argument(
"--output",
type=str,
default="output",
help="location of output",
)
args = parser.parse_known_args()[0]
save_runs = args.save_runs
force = args.force
batch_size = args.batch_size
images_data = args.data
output = args.output
output = Path(output)
output.mkdir(exist_ok=True)
locations = list(Path(images_data).glob("*/"))
# %%
for loc in tqdm(locations):
summary_file = output / f"{loc.stem}.summary.csv"
if not summary_file.exists() or force:
imgs = sorted(list(loc.glob("*.jpg")))
timestamps, counts = [], []
for batch_imgs in batch(imgs, batch_size):
model = YOLO("yolov8s.pt")
results = model(
batch_imgs,
classes=[2, 3, 5, 7],
save=save_runs,
exist_ok=True,
project=output,
name=loc.stem,
)
_timestamps = [
os.path.basename(r.path).split(".")[0].split("-")[1:] for r in results
]
_timestamps = [datetime(*[int(i) for i in t]) for t in _timestamps]
_counts = [len(r.boxes) for r in results]
timestamps.extend(_timestamps)
counts.extend(_counts)
df = pd.DataFrame(dict(ds=timestamps, count=counts))
df["hour"] = df["ds"].dt.hour
df["weekday"] = df["ds"].dt.day_name()
df.to_csv(
summary_file,
index=False,
)
df = pd.read_csv(summary_file)
cname = loc / "cname.txt"
if cname.exists():
title = open(cname).read()
else:
title = None
fig = plt.figure(figsize=(10, 6))
ax = sns.boxplot(data=df, x="hour", y="count")
if title:
ax.set_title(title)
fig.savefig(
output / f"{loc.stem}.summary.hour.png",
dpi=600,
bbox_inches="tight",
)
plt.close(fig)
fig = plt.figure(figsize=(10, 6))
order = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
]
ax = sns.boxplot(
data=df,
x="weekday",
y="count",
order=[d for d in order if d in list(df["weekday"].unique())],
)
if title:
ax.set_title(title)
fig.savefig(
output / f"{loc.stem}.summary.weekday.png",
dpi=600,
bbox_inches="tight",
)
plt.close(fig)