Skip to content

Commit 95d7a0d

Browse files
committed
added example for: TensorFlow Object Detection API
1 parent 6533985 commit 95d7a0d

15 files changed

+6512
-5
lines changed

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
![cat.jpg](https://raw.githubusercontent.com/php-opencv/php-opencv-examples/master/images/cat.jpg)
2020
Results: 87%: Egyptian cat, 4%: tabby, tabby cat, 2%: tiger cat
2121

22+
- [detect objects by dnn mobilenet](https://github.com/php-opencv/php-opencv-examples/blob/master/detect_objects_by_dnn_mobilenet.php)
23+
![detect_objects_by_dnn_mobilenet.png](https://raw.githubusercontent.com/php-opencv/php-opencv-examples/master/results/detect_objects_by_dnn_mobilenet.png)
24+
2225
#### Helper for autocomplete and highlighting in your IDE
2326
- [phpdoc file](https://github.com/php-opencv/php-opencv-examples/blob/master/phpdoc.php)
2427

Diff for: categorize_image_by_dnn_mobilenet.php renamed to classify_image_by_dnn_mobilenet.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use CV\Scalar;
44
use function CV\{imread, cvtColor};
55

6-
$categories = file('models/mobilenet/synset.txt');
6+
$categories = explode("\n", file_get_contents('models/mobilenet/classes.txt'));
77

88
$src = imread("images/cat.jpg"); // opencv loads image to matrix with BGR order
99
$src = cvtColor($src, CV\COLOR_BGR2RGB); // convert BGR to RGB
@@ -30,5 +30,5 @@
3030
$confidences = array_slice($confidences, 0, 5, true);
3131

3232
foreach ($confidences as $label => $confidence) {
33-
echo "$confidence%: {$categories[$label]}";
33+
echo "$confidence%: {$categories[$label]}\n";
3434
}

Diff for: detect_face_by_dnn_ssd.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,12 @@
2424
$confidence = $r->atIdx([0,0,$i,2]);
2525
//var_export($confidence);echo "\n";
2626
if ($confidence > 0.5) {
27-
rectangle($src, $r->atIdx([0,0,$i,3])*$src->cols, $r->atIdx([0,0,$i,4])*$src->rows, $r->atIdx([0,0,$i,5])*$src->cols, $r->atIdx([0,0,$i,6])*$src->rows, $scalar, 3);
27+
$startX = $r->atIdx([0,0,$i,3]) * $src->cols;
28+
$startY = $r->atIdx([0,0,$i,4]) * $src->rows;
29+
$endX = $r->atIdx([0,0,$i,5]) * $src->cols;
30+
$endY = $r->atIdx([0,0,$i,6]) * $src->rows;
31+
32+
rectangle($src, $startX, $startY, $endX, $endY, $scalar, 3);
2833
}
2934
}
3035

Diff for: detect_objects_by_dnn_mobilenet.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use CV\Scalar;
4+
use function CV\{imread, cvtColor};
5+
6+
//$categories = explode("\n", file_get_contents('models/ssd_mobilenet_v1_coco/classes.txt'));
7+
$categories = explode("\n", file_get_contents('models/ssdlite_mobilenet_v2_coco/classes.txt'));
8+
9+
$src = imread("images/objects.jpg"); // opencv loads image to matrix with BGR order
10+
//var_export($src);
11+
12+
$blob = \CV\DNN\blobFromImage($src, 0.017, new \CV\Size(300,300), new Scalar(127.5, 127.5, 127.5), true, false); // convert image to 4 dimensions matrix
13+
//var_export($blob);
14+
15+
//$net = \CV\DNN\readNetFromTensorflow('models/ssd_mobilenet_v1_coco/frozen_inference_graph.pb', 'models/ssd_mobilenet_v1_coco/ssd_mobilenet_v1_coco.pbtxt');
16+
$net = \CV\DNN\readNetFromTensorflow('models/ssdlite_mobilenet_v2_coco/frozen_inference_graph.pb', 'models/ssdlite_mobilenet_v2_coco/ssdlite_mobilenet_v2_coco.pbtxt');
17+
$net->setInput($blob, "");
18+
19+
$r = $net->forward();
20+
var_export($r);
21+
22+
$rectangles = [];
23+
for ($i = 0; $i < $r->shape[2]; $i++) {
24+
$classId = $r->atIdx([0,0,$i,1]);
25+
$confidence = intval($r->atIdx([0,0,$i,2]) * 100);
26+
if ($classId && $confidence > 30) {
27+
$startX = $r->atIdx([0,0,$i,3]) * $src->cols;
28+
$startY = $r->atIdx([0,0,$i,4]) * $src->rows;
29+
$endX = $r->atIdx([0,0,$i,5]) * $src->cols;
30+
$endY = $r->atIdx([0,0,$i,6]) * $src->rows;
31+
32+
$scalar = new Scalar(0, 0, 255);
33+
\CV\rectangle($src, $startX, $startY, $endX, $endY, $scalar, 2);
34+
35+
$text = "{$categories[$classId]} $confidence%";
36+
\CV\rectangle($src, $startX, $startY + 10, $startX + 20 * strlen($text), $startY - 30, new Scalar(255,255,255), -2);
37+
\CV\putText($src, "{$categories[$classId]} $confidence%", new \CV\Point($startX, $startY - 2), 0, 1.5, new Scalar(), 2);
38+
}
39+
}
40+
41+
\CV\imwrite("results/_detect_objects_by_dnn_mobilenet.png", $src);

Diff for: images/objects.jpg

120 KB
Loading

Diff for: models/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ Sources:
55
* openface - https://github.com/cmusatyalab/openface/blob/master/docs/models-and-accuracies.md
66
* ssd - https://github.com/opencv/opencv_3rdparty/tree/dnn_samples_face_detector_20170830
77
* waifu2x - https://github.com/HomeOfVapourSynthEvolution/VapourSynth-Waifu2x-caffe/tree/master/Waifu2x-caffe/models/upconv_7_anime_style_art_rgb
8-
* mobilenet - https://github.com/shicai/MobileNet-Caffe
8+
* mobilenet - https://github.com/shicai/MobileNet-Caffe
9+
* ssd_mobilenet_v1_coco - https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md
10+
* ssdlite_mobilenet_v2_coco - https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md
File renamed without changes.

Diff for: models/ssd_mobilenet_v1_coco/classes.txt

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
unclassified
2+
person
3+
bicycle
4+
car
5+
motorcycle
6+
airplane
7+
bus
8+
train
9+
truck
10+
boat
11+
traffic
12+
fire
13+
street
14+
stop
15+
parking
16+
bench
17+
bird
18+
cat
19+
dog
20+
horse
21+
sheep
22+
cow
23+
elephant
24+
bear
25+
zebra
26+
giraffe
27+
hat
28+
backpack
29+
umbrella
30+
shoe
31+
eye
32+
handbag
33+
tie
34+
suitcase
35+
frisbee
36+
skis
37+
snowboard
38+
sports
39+
kite
40+
baseball
41+
baseball
42+
skateboard
43+
surfboard
44+
tennis
45+
bottle
46+
plate
47+
wine
48+
cup
49+
fork
50+
knife
51+
spoon
52+
bowl
53+
banana
54+
apple
55+
sandwich
56+
orange
57+
broccoli
58+
carrot
59+
hot
60+
pizza
61+
donut
62+
cake
63+
chair
64+
couch
65+
potted
66+
bed
67+
mirror
68+
dining
69+
window
70+
desk
71+
toilet
72+
door
73+
tv
74+
laptop
75+
mouse
76+
remote
77+
keyboard
78+
cell
79+
microwave
80+
oven
81+
toaster
82+
sink
83+
refrigerator
84+
blender
85+
book
86+
clock
87+
vase
88+
scissors
89+
teddy
90+
hair
91+
toothbrush
92+
hair
93+
banner
94+
blanket
95+
branch
96+
bridge
97+
building-other
98+
bush
99+
cabinet
100+
cage
101+
cardboard
102+
carpet
103+
ceiling-other
104+
ceiling-tile
105+
cloth
106+
clothes
107+
clouds
108+
counter
109+
cupboard
110+
curtain
111+
desk-stuff
112+
dirt
113+
door-stuff
114+
fence
115+
floor-marble
116+
floor-other
117+
floor-stone
118+
floor-tile
119+
floor-wood
120+
flower
121+
fog
122+
food-other
123+
fruit
124+
furniture-other
125+
grass
126+
gravel
127+
ground-other
128+
hill
129+
house
130+
leaves
131+
light
132+
mat
133+
metal
134+
mirror-stuff
135+
moss
136+
mountain
137+
mud
138+
napkin
139+
net
140+
paper
141+
pavement
142+
pillow
143+
plant-other
144+
plastic
145+
platform
146+
playingfield
147+
railing
148+
railroad
149+
river
150+
road
151+
rock
152+
roof
153+
rug
154+
salad
155+
sand
156+
sea
157+
shelf
158+
sky-other
159+
skyscraper
160+
snow
161+
solid-other
162+
stairs
163+
stone
164+
straw
165+
structural-other
166+
table
167+
tent
168+
textile-other
169+
towel
170+
tree
171+
vegetable
172+
wall-brick
173+
wall-concrete
174+
wall-other
175+
wall-panel
176+
wall-stone
177+
wall-tile
178+
wall-wood
179+
water-other
180+
waterdrops
181+
window-blind
182+
window-other
183+
wood
27.8 MB
Binary file not shown.

0 commit comments

Comments
 (0)