imageAi.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import os
  2. # import tensorflow as tf
  3. # gpus = tf.config.experimental.list_physical_devices('GPU')
  4. # if gpus:
  5. # try:
  6. # # Currently, memory growth needs to be the same across GPUs
  7. # for gpu in gpus:
  8. # tf.config.experimental.set_memory_growth(gpu, True)
  9. # logical_gpus = tf.config.experimental.list_logical_devices('GPU')
  10. # print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
  11. # except RuntimeError as e:
  12. # # Memory growth must be set before GPUs have been initialized
  13. # print(e)
  14. # gpus = tf.config.list_physical_devices('GPU')
  15. # tf.config.experimental.set_virtual_device_configuration(
  16. # gpus[0],
  17. # [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=1024)])
  18. from imageai.Detection import ObjectDetection
  19. execution_path = os.getcwd()
  20. detector = ObjectDetection()
  21. detector.setModelTypeAsYOLOv3()
  22. # detector.setModelTypeAsRetinaNet()
  23. detector.setModelPath(os.path.join(execution_path, 'yolo.h5'))
  24. detector.loadModel()
  25. detections = detector.detectObjectsFromImage(
  26. input_image=os.path.join(execution_path, 'circuit_board_img_result.png'),
  27. output_image_path=os.path.join(execution_path, 'snapshot_detected.jpg'),
  28. minimum_percentage_probability=30
  29. )
  30. print(detections, end='\n')
  31. for eachObject in detections:
  32. print(eachObject['name'], ': ', eachObject['percentage_probability'])
  33. # print("--------------------------------")