Skip to content

Instantly share code, notes, and snippets.

@andreconghau
Created August 22, 2022 08:21
Show Gist options
  • Save andreconghau/fcbe5397520d940d79725f635f22ca1c to your computer and use it in GitHub Desktop.
Save andreconghau/fcbe5397520d940d79725f635f22ca1c to your computer and use it in GitHub Desktop.
simple_detection.py
from imageai.Detection import ObjectDetection
import os
execution_path = os.getcwd()
print(execution_path)
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path, "resnet50_coco_best_v2.1.0.h5")) # Download the model via this link https://github.com/OlafenwaMoses/ImageAI/releases/tag/1.0
detector.loadModel()
input_image=os.path.join(execution_path, "02.png")
output_image_path=os.path.join(execution_path, "02_detected.jpg")
print(input_image)
print(output_image_path)
detections = detector.detectObjectsFromImage(input_image, output_image_path, minimum_percentage_probability=40)
for eachObject in detections:
print(eachObject["name"], " : ", eachObject["percentage_probability"], " : ", eachObject["box_points"] )
print("--------------------------------")
@andreconghau
Copy link
Author

Installation

https://imageai.readthedocs.io/en/latest/

Python required 3.7.6

Note Dependence

python -m pip install tensorflow==2.4
python -m pip install numpy==1.19.3

Model TRANING

resnet50_coco_best_v2.1.0.h5

Input Model

02

Output Model

02_detected

@andreconghau
Copy link
Author

Console

2022-08-22 15:17:29.339868: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2022-08-22 15:17:29.340791: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
WARNING:tensorflow:No training configuration found in the save file, so the model was *not* compiled. Compile it manually.
/Users/andre/www/2022/python/02.png
/Users/andre/www/2022/python/02_detected.jpg
2022-08-22 15:17:36.747924: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2)
chair  :  87.15589642524719  :  [45, 1094, 445, 1529]
--------------------------------
cup  :  82.60263204574585  :  [710, 1191, 811, 1326]
--------------------------------
person  :  81.01999163627625  :  [1325, 664, 1634, 1084]
--------------------------------
person  :  71.21261954307556  :  [99, 716, 706, 1512]
--------------------------------
person  :  69.56616044044495  :  [367, 660, 743, 1142]
--------------------------------
person  :  67.64578223228455  :  [1425, 702, 1893, 1439]
--------------------------------
person  :  65.43447971343994  :  [696, 614, 933, 898]
--------------------------------
person  :  65.25717973709106  :  [879, 608, 985, 762]
--------------------------------
person  :  62.35237717628479  :  [630, 626, 862, 937]
--------------------------------
dining table  :  61.79282069206238  :  [472, 742, 1567, 1529]
--------------------------------
person  :  59.45751667022705  :  [915, 599, 1010, 737]
--------------------------------
person  :  58.5770308971405  :  [515, 666, 795, 1083]
--------------------------------
person  :  56.713175773620605  :  [955, 599, 1027, 730]
--------------------------------
bowl  :  56.48313760757446  :  [1004, 1000, 1123, 1087]
--------------------------------
person  :  55.42140007019043  :  [1217, 652, 1367, 903]
--------------------------------
person  :  54.67312932014465  :  [800, 608, 978, 804]
--------------------------------
person  :  53.998374938964844  :  [1097, 602, 1173, 715]
--------------------------------
bottle  :  45.37689685821533  :  [1036, 867, 1094, 1005]
--------------------------------
person  :  44.56872642040253  :  [848, 610, 943, 769]
--------------------------------
person  :  44.50908005237579  :  [1363, 652, 1455, 805]
--------------------------------
person  :  44.43022310733795  :  [1287, 657, 1456, 927]
--------------------------------
cup  :  43.090444803237915  :  [1008, 1001, 1122, 1093]
--------------------------------
backpack  :  43.00024211406708  :  [1703, 1032, 2026, 1522]
--------------------------------
person  :  42.201340198516846  :  [1330, 678, 1767, 1275]
--------------------------------
cup  :  41.91853702068329  :  [978, 1050, 1039, 1115]
--------------------------------
cup  :  41.41806662082672  :  [1026, 1087, 1089, 1156]
--------------------------------
person  :  40.38589000701904  :  [1170, 615, 1237, 794]
--------------------------------
person  :  40.259623527526855  :  [1299, 645, 1376, 792]
--------------------------------
person  :  40.014901757240295  :  [225, 665, 669, 1280]
--------------------------------

@andreconghau
Copy link
Author

#Custom Filter get only person or something
https://imageai.readthedocs.io/en/latest/detection/index.html

There are 80 possible objects that you can detect with the
ObjectDetection class, and they are as seen below.

person,   bicycle,   car,   motorcycle,   airplane,
bus,   train,   truck,   boat,   traffic light,   fire hydrant,   stop_sign,
parking meter,   bench,   bird,   cat,   dog,   horse,   sheep,   cow,   elephant,   bear,   zebra,
giraffe,   backpack,   umbrella,   handbag,   tie,   suitcase,   frisbee,   skis,   snowboard,
sports ball,   kite,   baseball bat,   baseball glove,   skateboard,   surfboard,   tennis racket,
bottle,   wine glass,   cup,   fork,   knife,   spoon,   bowl,   banana,   apple,   sandwich,   orange,
broccoli,   carrot,   hot dog,   pizza,   donot,   cake,   chair,   couch,   potted plant,   bed,
dining table,   toilet,   tv,   laptop,   mouse,   remote,   keyboard,   cell phone,   microwave,
oven,   toaster,   sink,   refrigerator,   book,   clock,   vase,   scissors,   teddy bear,   hair dryer,
toothbrush.

To detect only some of the objects above, you will need to call the CustomObjects function and set the name of the
object(s) yiu want to detect to through. The rest are False by default. In below example, we detected only chose detect only person and dog.

output_custom_image_path=os.path.join(execution_path, "02_custom_detected.jpg")
custom = detector.CustomObjects(person=True)

detections_custom = detector.detectCustomObjectsFromImage( custom_objects=custom,input_image=input_image,output_image_path=output_custom_image_path,minimum_percentage_probability=55)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment