Skip to content

Commit e71a2e4

Browse files
authored
Update to openpifpaf v0.12.10 (#63)
* Updated for openpifpaf v0.12.10 * Linting and better logging * clean up * better comment * fix
1 parent ddeb860 commit e71a2e4

File tree

4 files changed

+46
-48
lines changed

4 files changed

+46
-48
lines changed

README.md

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ If you provide a ground-truth json file to compare the predictions of the networ
125125
For an example image, run the following command:
126126

127127
```sh
128-
python -m monoloco.run predict docs/test_002282.png \
128+
python3 -m monoloco.run predict docs/test_002282.png \
129129
--path_gt names-kitti-200615-1022.json \
130130
-o <output directory> \
131131
--long-edge <rescale the image by providing dimension of long side>
@@ -140,7 +140,7 @@ To show all the instances estimated by MonoLoco add the argument `--show_all` to
140140

141141
It is also possible to run [openpifpaf](https://github.com/vita-epfl/openpifpaf) directly
142142
by using `--mode keypoints`. All the other pifpaf arguments are also supported
143-
and can be checked with `python -m monoloco.run predict --help`.
143+
and can be checked with `python3 -m monoloco.run predict --help`.
144144

145145
![predict](docs/out_test_002282_pifpaf.jpg)
146146

@@ -178,7 +178,7 @@ To visualize social distancing compliance, simply add the argument `social_dista
178178
Threshold distance and radii (for F-formations) can be set using `--threshold-dist` and `--radii`, respectively.
179179

180180
For more info, run:
181-
`python -m monoloco.run predict --help`
181+
`python3 -m monoloco.run predict --help`
182182

183183
**Examples** <br>
184184
An example from the Collective Activity Dataset is provided below.
@@ -188,11 +188,11 @@ An example from the Collective Activity Dataset is provided below.
188188
To visualize social distancing run the below, command:
189189

190190
```sh
191-
pip install scipy
191+
pip3 install scipy
192192
```
193193

194194
```sh
195-
python -m monoloco.run predict docs/test_frame0032.jpg \
195+
python3 -m monoloco.run predict docs/test_frame0032.jpg \
196196
--activities social_distance --output_types front bird
197197
```
198198

@@ -203,13 +203,14 @@ To detect raised hand, you can add the argument `--activities raise_hand` to the
203203

204204
For example, the below image is obtained with:
205205
```sh
206-
python -m monoloco.run predict --activities raise_hand social_distance output_types front
206+
python3 -m monoloco.run predict docs/raising_hand.jpg \
207+
--activities raise_hand social_distance --output_types front
207208
```
208209

209210
<img src="docs/out_raising_hand.jpg.front.jpg" width="500"/>
210211

211212
For more info, run:
212-
`python -m monoloco.run predict --help`
213+
`python3 -m monoloco.run predict --help`
213214

214215
## D) Orientation and Bounding Box dimensions
215216
The network estimates orientation and box dimensions as well. Results are saved in a json file when using the command
@@ -218,11 +219,14 @@ The network estimates orientation and box dimensions as well. Results are saved
218219

219220
## E) Webcam
220221
You can use the webcam as input by using the `--webcam` argument. By default the `--z_max` is set to 10 while using the webcam and the `--long-edge` is set to 144. If multiple webcams are plugged in you can choose between them using `--camera`, for instance to use the second camera you can add `--camera 1`.
221-
222+
You also need to install `opencv-python` to use this feature :
223+
```sh
224+
pip3 install opencv-python
225+
```
222226
Example command:
223227

224228
```sh
225-
python -m monoloco.run predict --webcam \
229+
python3 -m monoloco.run predict --webcam \
226230
--activities raise_hand social_distance
227231
```
228232

@@ -231,21 +235,22 @@ We train on the KITTI dataset (MonoLoco/Monoloco++/MonStereo) or the nuScenes da
231235

232236
Results for [MonoLoco++](###Tables) are obtained with:
233237

234-
```
235-
python -m monoloco.run train --joints data/arrays/joints-kitti-mono-210422-1600.json
238+
```sh
239+
python3 -m monoloco.run train --joints data/arrays/joints-kitti-mono-210422-1600.json
236240
```
237241

238242
While for the [MonStereo](###Tables) results run:
239243

240244
```sh
241-
python -m monoloco.run train --joints data/arrays/joints-kitti-stereo-210422-1601.json --lr 0.003 --mode stereo
245+
python3 -m monoloco.run train --joints data/arrays/joints-kitti-stereo-210422-1601.json \
246+
--lr 0.003 --mode stereo
242247
```
243248

244249
If you are interested in the original results of the MonoLoco ICCV article (now improved with MonoLoco++), please refer to the tag v0.4.9 in this repository.
245250

246251
Finally, for a more extensive list of available parameters, run:
247252

248-
`python -m monstereo.run train --help`
253+
`python3 -m monstereo.run train --help`
249254

250255
<br />
251256

@@ -284,7 +289,7 @@ Download kitti images (from left and right cameras), ground-truth files (labels)
284289
The network takes as inputs 2D keypoints annotations. To create them run PifPaf over the saved images:
285290

286291
```sh
287-
python -m openpifpaf.predict \
292+
python3 -m openpifpaf.predict \
288293
--glob "data/kitti/images/*.png" \
289294
--json-output <directory to contain predictions> \
290295
--checkpoint=shufflenetv2k30 \
@@ -306,12 +311,12 @@ Once this step is complete, the below commands transform all the annotations int
306311

307312
For MonoLoco++:
308313
```sh
309-
python -m monoloco.run prep --dir_ann <directory that contains annotations>
314+
python3 -m monoloco.run prep --dir_ann <directory that contains annotations>
310315
```
311316

312317
For MonStereo:
313318
```sh
314-
python -m monoloco.run prep --mode stereo --dir_ann <directory that contains left annotations>
319+
python3 -m monoloco.run prep --mode stereo --dir_ann <directory that contains left annotations>
315320
```
316321

317322
## Collective Activity Dataset
@@ -341,7 +346,7 @@ which for example change the name of all the jpg images in that folder adding th
341346
Pifpaf annotations should also be saved in a single folder and can be created with:
342347

343348
```sh
344-
python -m openpifpaf.predict \
349+
python3 -m openpifpaf.predict \
345350
--glob "data/collective_activity/images/*.jpg" \
346351
--checkpoint=shufflenetv2k30 \
347352
--instance-threshold=0.05 --seed-threshold 0.05 \--force-complete-pose \
@@ -381,7 +386,7 @@ To include also geometric baselines and MonoLoco, download a monoloco model, sav
381386
The evaluation file will run the model over all the annotations and compare the results with KITTI ground-truth and the downloaded baselines. For this run:
382387

383388
```sh
384-
python -m monoloco.run eval \
389+
python3 -m monoloco.run eval \
385390
--dir_ann <annotation directory> \
386391
--model data/outputs/monoloco_pp-210422-1601.pkl \
387392
--generate \
@@ -411,7 +416,7 @@ Evaluation on this dataset is done with models trained on either KITTI or nuScen
411416
For optimal performances, we suggest the model trained on nuScenes teaser.
412417

413418
```sh
414-
python -m monstereo.run eval \
419+
python3 -m monstereo.run eval \
415420
--activity \
416421
--dataset collective \
417422
--model <path to the model> \

monoloco/predict.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import PIL
1919
import openpifpaf
2020
import openpifpaf.datasets as datasets
21-
from openpifpaf.predict import processor_factory, preprocess_factory
2221
from openpifpaf import decoder, network, visualizer, show, logger
2322
try:
2423
import gdown
@@ -162,12 +161,11 @@ def predict(args):
162161
n_dropout=args.n_dropout,
163162
p_dropout=args.dropout)
164163

165-
# data
166-
processor, pifpaf_model = processor_factory(args)
167-
preprocess = preprocess_factory(args)
164+
# for openpifpaf predicitons
165+
predictor = openpifpaf.Predictor(checkpoint=args.checkpoint)
168166

169167
# data
170-
data = datasets.ImageList(args.images, preprocess=preprocess)
168+
data = datasets.ImageList(args.images, preprocess=predictor.preprocess)
171169
if args.mode == 'stereo':
172170
assert len(
173171
data.image_paths) % 2 == 0, "Odd number of images in a stereo setting"
@@ -176,22 +174,19 @@ def predict(args):
176174
data, batch_size=args.batch_size, shuffle=False,
177175
pin_memory=False, collate_fn=datasets.collate_images_anns_meta)
178176

179-
for batch_i, (image_tensors_batch, _, meta_batch) in enumerate(data_loader):
180-
pred_batch = processor.batch(
181-
pifpaf_model, image_tensors_batch, device=args.device)
177+
for batch_i, (_, _, meta_batch) in enumerate(data_loader):
182178

183179
# unbatch (only for MonStereo)
184-
for idx, (pred, meta) in enumerate(zip(pred_batch, meta_batch)):
180+
for idx, (preds, _, meta) in enumerate(predictor.dataset(data)):
185181
LOG.info('batch %d: %s', batch_i, meta['file_name'])
186-
pred = [ann.inverse_transform(meta) for ann in pred]
187182

188183
# Load image and collect pifpaf results
189184
if idx == 0:
190185
with open(meta_batch[0]['file_name'], 'rb') as f:
191186
cpu_image = PIL.Image.open(f).convert('RGB')
192187
pifpaf_outs = {
193-
'pred': pred,
194-
'left': [ann.json_data() for ann in pred],
188+
'pred': preds,
189+
'left': [ann.json_data() for ann in preds],
195190
'image': cpu_image}
196191

197192
# Set output image name
@@ -208,7 +203,7 @@ def predict(args):
208203

209204
# Only for MonStereo
210205
else:
211-
pifpaf_outs['right'] = [ann.json_data() for ann in pred]
206+
pifpaf_outs['right'] = [ann.json_data() for ann in preds]
212207

213208
# 3D Predictions
214209
if args.mode != 'keypoints':

monoloco/visuals/webcam.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
except ImportError:
1818
cv2 = None
1919

20+
import openpifpaf
2021
from openpifpaf import decoder, network, visualizer, show, logger
2122
import openpifpaf.datasets as datasets
22-
from openpifpaf.predict import processor_factory, preprocess_factory
2323

2424
from ..visuals import Printer
2525
from ..network import Loco
@@ -73,15 +73,16 @@ def factory_from_args(args):
7373
def webcam(args):
7474

7575
assert args.mode in 'mono'
76+
assert cv2
7677

7778
args, dic_models = factory_from_args(args)
7879

7980
# Load Models
8081
net = Loco(model=dic_models[args.mode], mode=args.mode, device=args.device,
8182
n_dropout=args.n_dropout, p_dropout=args.dropout)
8283

83-
processor, pifpaf_model = processor_factory(args)
84-
preprocess = preprocess_factory(args)
84+
# for openpifpaf predicitons
85+
predictor = openpifpaf.Predictor(checkpoint=args.checkpoint)
8586

8687
# Start recording
8788
cam = cv2.VideoCapture(args.camera)
@@ -93,36 +94,33 @@ def webcam(args):
9394
scale = (args.long_edge)/frame.shape[0]
9495
image = cv2.resize(frame, None, fx=scale, fy=scale)
9596
height, width, _ = image.shape
96-
print('resized image size: {}'.format(image.shape))
97+
LOG.debug('resized image size: {}'.format(image.shape))
9798
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
9899
pil_image = Image.fromarray(image)
99100

100101
data = datasets.PilImageList(
101-
[pil_image], preprocess=preprocess)
102+
[pil_image], preprocess=predictor.preprocess)
102103

103104
data_loader = torch.utils.data.DataLoader(
104105
data, batch_size=1, shuffle=False,
105106
pin_memory=False, collate_fn=datasets.collate_images_anns_meta)
106107

107-
for (image_tensors_batch, _, meta_batch) in data_loader:
108-
pred_batch = processor.batch(
109-
pifpaf_model, image_tensors_batch, device=args.device)
108+
for (_, _, _) in data_loader:
110109

111-
for idx, (pred, meta) in enumerate(zip(pred_batch, meta_batch)):
112-
pred = [ann.inverse_transform(meta) for ann in pred]
110+
for idx, (preds, _, _) in enumerate(predictor.dataset(data)):
113111

114112
if idx == 0:
115113
pifpaf_outs = {
116-
'pred': pred,
117-
'left': [ann.json_data() for ann in pred],
114+
'pred': preds,
115+
'left': [ann.json_data() for ann in preds],
118116
'image': image}
119117

120118
if not ret:
121119
break
122120
key = cv2.waitKey(1)
123121
if key % 256 == 27:
124122
# ESC pressed
125-
print("Escape hit, closing...")
123+
LOG.info("Escape hit, closing...")
126124
break
127125

128126
kk, dic_gt = factory_for_gt(pil_image.size, focal_length=args.focal)
@@ -140,11 +138,11 @@ def webcam(args):
140138
visualizer_mono = Visualizer(kk, args)(pil_image) # create it with the first image
141139
visualizer_mono.send(None)
142140

143-
print(dic_out)
141+
LOG.debug(dic_out)
144142
visualizer_mono.send((pil_image, dic_out, pifpaf_outs))
145143

146144
end = time.time()
147-
print("run-time: {:.2f} ms".format((end-start)*1000))
145+
LOG.info("run-time: {:.2f} ms".format((end-start)*1000))
148146

149147
cam.release()
150148

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
zip_safe=False,
3333

3434
install_requires=[
35-
'openpifpaf>=v0.12.1',
35+
'openpifpaf>=v0.12.10',
3636
'matplotlib',
3737
],
3838
extras_require={

0 commit comments

Comments
 (0)