Skip to content

Instantly share code, notes, and snippets.

@OlafenwaMoses
Last active August 13, 2022 17:11
Show Gist options
  • Save OlafenwaMoses/38e68a0f3bcb7350785b67d2e6bc06ce to your computer and use it in GitHub Desktop.
Save OlafenwaMoses/38e68a0f3bcb7350785b67d2e6bc06ce to your computer and use it in GitHub Desktop.
Object detection in 10 lines of code
from imageai.Detection import ObjectDetection
import os
execution_path = os.getcwd()
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.1.0.h5"))
detector.loadModel()
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "image.jpg"), output_image_path=os.path.join(execution_path , "imagenew.jpg"))
for eachObject in detections:
print(eachObject["name"] , " : " , eachObject["percentage_probability"] )
@akfaisel
Copy link

akfaisel commented Feb 26, 2020

I'm getting the following error message. I'm using Python 3.7 and Tensorflow 2.0

Traceback (most recent call last):
  File "FirstDetection.py", line 6, in <module>
    detector = ObjectDetection()
  File "D:\Anaconda3\lib\site-packages\imageai\Detection\__init__.py", line 88, in __init__
    self.sess = K.get_session()
  File "D:\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 174, in get_session
    default_session = tf.get_default_session()
AttributeError: module 'tensorflow' has no attribute 'get_default_session'

I solved the above error by changing self.sess = K.get_session() to self.sess = tf.compat.v1.Session() in lib\site-packages\imageai\Detection_init_.py

@ShettyMahalakshmi
Copy link

OSError Traceback (most recent call last)
in
7 detector.setModelTypeAsRetinaNet()
8 detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))
----> 9 detector.loadModel()
10 detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "image.jpg"), output_image_path=os.path.join(execution_path , "imagenew.jpg"))
11

C:\ProgramData\Anaconda3\lib\site-packages\imageai\Detection_init_.py in loadModel(self, detection_speed)
189 elif (self.__modelType == "retinanet"):
190 model = resnet50_retinanet(num_classes=80)
--> 191 model.load_weights(self.modelPath)
192 self.__model_collection.append(model)
193 self.__modelLoaded = True

C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\network.py in load_weights(self, filepath, by_name, skip_mismatch, reshape)
1155 if h5py is None:
1156 raise ImportError('load_weights requires h5py.')
-> 1157 with h5py.File(filepath, mode='r') as f:
1158 if 'layer_names' not in f.attrs and 'model_weights' in f:
1159 f = f['model_weights']

C:\ProgramData\Anaconda3\lib\site-packages\h5py_hl\files.py in init(self, name, mode, driver, libver, userblock_size, swmr, **kwds)
310 with phil:
311 fapl = make_fapl(driver, libver, **kwds)
--> 312 fid = make_fid(name, mode, userblock_size, fapl, swmr=swmr)
313
314 if swmr_support:

C:\ProgramData\Anaconda3\lib\site-packages\h5py_hl\files.py in make_fid(name, mode, userblock_size, fapl, fcpl, swmr)
140 if swmr and swmr_support:
141 flags |= h5f.ACC_SWMR_READ
--> 142 fid = h5f.open(name, flags, fapl=fapl)
143 elif mode == 'r+':
144 fid = h5f.open(name, h5f.ACC_RDWR, fapl=fapl)

h5py_objects.pyx in h5py._objects.with_phil.wrapper()

h5py_objects.pyx in h5py._objects.with_phil.wrapper()

h5py\h5f.pyx in h5py.h5f.open()

OSError: Unable to open file (file signature not found)

@aldowiratama
Copy link

!pip install tensorflow
!pip install opencv-python
!pip install keras
!pip install numpy
!pip install imageai

from imageai.Detection import ObjectDetection
import os

execution_path = os.getcwd()

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel()
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "1598954196.053694.jpg"), output_image_path=os.path.join(execution_path , "an.jpg"))

for eachObject in detections:
print(eachObject["name"] , " : " , eachObject["percentage_probability"] )

AttributeError Traceback (most recent call last)
in
4 execution_path = os.getcwd()
5
----> 6 detector = ObjectDetection()
7 detector.setModelTypeAsRetinaNet()
8 detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))

c:\users\asus\appdata\local\programs\python\python38\lib\site-packages\imageai\Detection_init_.py in init(self)
86 self.__yolo_model_image_size = (416, 416)
87 self.__yolo_boxes, self.__yolo_scores, self.__yolo_classes = "", "", ""
---> 88 self.sess = K.get_session()
89
90 # Unique instance variables for TinyYOLOv3.

AttributeError: module 'keras.backend' has no attribute 'get_session'

@gdinn
Copy link

gdinn commented Oct 3, 2020

!pip install tensorflow
!pip install opencv-python
!pip install keras
!pip install numpy
!pip install imageai

from imageai.Detection import ObjectDetection
import os

execution_path = os.getcwd()

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel()
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "1598954196.053694.jpg"), output_image_path=os.path.join(execution_path , "an.jpg"))

for eachObject in detections:
print(eachObject["name"] , " : " , eachObject["percentage_probability"] )

AttributeError Traceback (most recent call last)
in
4 execution_path = os.getcwd()
5
----> 6 detector = ObjectDetection()
7 detector.setModelTypeAsRetinaNet()
8 detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))

c:\users\asus\appdata\local\programs\python\python38\lib\site-packages\imageai\Detection__init__.py in init(self)
86 self.__yolo_model_image_size = (416, 416)
87 self.__yolo_boxes, self.__yolo_scores, self.__yolo_classes = "", "", ""
---> 88 self.sess = K.get_session()
89
90 # Unique instance variables for TinyYOLOv3.

AttributeError: module 'keras.backend' has no attribute 'get_session'

same here

@Savan2708
Copy link

I don't want to save images that can not be detected so what can I do ?

@rawstar134
Copy link

To detect the object from the image from scratch using python; Click here I found the best article https://debuggingsolution.blogspot.com/2022/02/object-detection-from-scratch-in-python.html

@RiderArun
Copy link

Here's what the above code is doing:

  1. We first import the ObjectDetection class from the ImageAI library.
  2. We then create an instance of the ObjectDetection class and set the model type to RetinaNet.
  3. We then set the model path to the path of the RetinaNet model file that we downloaded earlier.
  4. We then load the model into the ObjectDetection class instance.
  5. We then call the detectObjectsFromImage method, passing in the input image path and the output image path.
  6. We then print the name of the object and the percentage probability that the object detected is the correct one.

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