Attribute error while integrating Google TTS with YOLOv8

huangapple go评论109阅读模式
英文:

Attribute error while integrating Google TTS with YOLOv8

问题

以下是您要翻译的内容:

"My project aims to detect object labels and coordinates and then convert them into a string which is converted into voice using gTTS but I keep getting an attribute error in the prediction labels. I am new to this framework, any help will be appreciated."

"Code:"

  1. import cv2
  2. from gtts import gTTS
  3. import os
  4. from ultralytics import YOLO
  5. def convert_labels_to_text(labels):
  6. text = ", ".join(labels)
  7. return text
  8. class YOLOWithLabels(YOLO):
  9. def __call__(self, frame):
  10. results = super().__call__(frame)
  11. labels = results.pred[0].get_field("labels").tolist()
  12. annotated_frame = results.render()
  13. return annotated_frame, labels
  14. cap = cv2.VideoCapture(0)
  15. model = YOLOWithLabels('yolov8n.pt')
  16. while cap.isOpened():
  17. success, frame = cap.read()
  18. if success:
  19. annotated_frame, labels = model(frame)
  20. message = convert_labels_to_text(labels)
  21. tts_engine = gTTS(text=message) # Initialize gTTS with the message
  22. tts_engine.save("output.mp3")
  23. os.system("output.mp3")
  24. cv2.putText(annotated_frame, message, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
  25. cv2.imshow("YOLOv8 Inference", annotated_frame)
  26. if cv2.waitKey(1) & 0xFF == ord("q"):
  27. break
  28. else:
  29. break
  30. cap.release()
  31. cv2.destroyAllWindows()

"Error"

  1. File "C:\Users\alien\Desktop\YOLOv8 project files\gtts service\testservice.py", line 13, in __call__
  2. labels = results.pred[0].get_field("labels").tolist()
  3. ^^^^^^^^^^^^
  4. AttributeError: 'list' object has no attribute 'pred'
  5. print(results)
  6. ~~~
  7. orig_shape: (480, 640)
  8. path: 'image0.jpg'
  9. probs: None
  10. save_dir: None
  11. speed: {'preprocess': 3.1604766845703125, 'inference': 307.905912399292, 'postprocess': 2.8924942016601562}]
  12. 0: 480x640 1 person, 272.4ms
  13. Speed: 3.0ms preprocess, 272.4ms inference, 4.0ms postprocess per image at shape (1, 3, 640, 640)
  14. [ultralytics.yolo.engine.results.Results object with attributes:
  15. boxes: ultralytics.yolo.engine.results.Boxes object
  16. keypoints: None
  17. keys: ['boxes']
  18. masks: None
  19. names: {0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus', 6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant', 11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat', 16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear', 22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag', 27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard', 32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove', 36: 'skateboard', 37: 'surfboard', 38: 'tennis racket', 39: 'bottle', 40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl', 46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli', 51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut', 55: 'cake', 56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table', 61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard', 67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink', 72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors', 77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush.'}
  20. orig_img: array([[[168, 167, 166],
  21. [165, 165, 165],
  22. [165, 166, 167],
  23. ...,
  24. [183, 186, 178],
  25. [183, 186, 178],
  26. [184, 187, 179]],
  27. [[168, 167, 165],
  28. [166, 165, 165],
  29. [166, 167, 166],
  30. ...,
  31. [184, 187, 179],
  32. [183, 186, 178],
  33. [184, 187, 179]],
  34. [[168, 167, 164],
  35. [167, 167, 164],
  36. [167, 167, 165],
  37. ...,
  38. [184, 187, 178],
  39. [184, 187, 179],
  40. [183, 186, 178]],
  41. ...,
  42. [[196, 192, 185],
  43. [196, 192, 185],
  44. [196, 192, 185],
  45. ...,
  46. [ 25, 29, 38],
  47. [ 22, 25, 35],
  48. [ 20, 24, 34]],
  49. [[199, 195, 187],
  50. [197, 193, 186],
  51. [197, 193, 186],
  52. ...,
  53. [ 23, 26, 35],
  54. [ 22, 25, 35],
  55. [ 22, 25, 35]],
  56. [[199, 195, 187],
  57. [199, 195, 187],
  58. [199, 195, 187],
  59. ...,
  60. [ 20, 24, 33],
  61. [ 19
  62. <details>
  63. <summary>英文:</summary>
  64. My project aims to detect object labels and coordinates and then convert them into a string which is converted into voice using gTTS but I keep getting an attribute error in the prediction labels. I am new to this framework, any help will be appreciated.
  65. Code:
  66. import cv2
  67. from gtts import gTTS
  68. import os
  69. from ultralytics import YOLO
  70. def convert_labels_to_text(labels):
  71. text = &quot;, &quot;.join(labels)
  72. return text
  73. class YOLOWithLabels(YOLO):
  74. def __call__(self, frame):
  75. results = super().__call__(frame)
  76. labels = results.pred[0].get_field(&quot;labels&quot;).tolist()
  77. annotated_frame = results.render()
  78. return annotated_frame, labels
  79. cap = cv2.VideoCapture(0)
  80. model = YOLOWithLabels(&#39;yolov8n.pt&#39;)
  81. while cap.isOpened():
  82. success, frame = cap.read()
  83. if success:
  84. annotated_frame, labels = model(frame)
  85. message = convert_labels_to_text(labels)
  86. tts_engine = gTTS(text=message) # Initialize gTTS with the message
  87. tts_engine.save(&quot;output.mp3&quot;)
  88. os.system(&quot;output.mp3&quot;)
  89. cv2.putText(annotated_frame, message, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
  90. cv2.imshow(&quot;YOLOv8 Inference&quot;, annotated_frame)
  91. if cv2.waitKey(1) &amp; 0xFF == ord(&quot;q&quot;):
  92. break
  93. else:
  94. break
  95. cap.release()
  96. cv2.destroyAllWindows()
  97. Error
  98. File &quot;C:\Users\alien\Desktop\YOLOv8 project files\gtts service\testservice.py&quot;, line 13, in __call__
  99. labels = results.pred[0].get_field(&quot;labels&quot;).tolist()
  100. ^^^^^^^^^^^^
  101. AttributeError: &#39;list&#39; object has no attribute &#39;pred&#39;
  102. print(results)
  103. ~~~
  104. orig_shape: (480, 640)
  105. path: &#39;image0.jpg&#39;
  106. probs: None
  107. save_dir: None
  108. speed: {&#39;preprocess&#39;: 3.1604766845703125, &#39;inference&#39;: 307.905912399292, &#39;postprocess&#39;: 2.8924942016601562}]
  109. 0: 480x640 1 person, 272.4ms
  110. Speed: 3.0ms preprocess, 272.4ms inference, 4.0ms postprocess per image at shape (1, 3, 640, 640)
  111. [ultralytics.yolo.engine.results.Results object with attributes:
  112. boxes: ultralytics.yolo.engine.results.Boxes object
  113. keypoints: None
  114. keys: [&#39;boxes&#39;]
  115. masks: None
  116. names: {0: &#39;person&#39;, 1: &#39;bicycle&#39;, 2: &#39;car&#39;, 3: &#39;motorcycle&#39;, 4: &#39;airplane&#39;, 5: &#39;bus&#39;, 6: &#39;train&#39;, 7: &#39;truck&#39;, 8: &#39;boat&#39;, 9: &#39;traffic light&#39;, 10: &#39;fire hydrant&#39;, 11: &#39;stop sign&#39;, 12: &#39;parking meter&#39;, 13: &#39;bench&#39;, 14: &#39;bird&#39;, 15: &#39;cat&#39;, 16: &#39;dog&#39;, 17: &#39;horse&#39;, 18: &#39;sheep&#39;, 19: &#39;cow&#39;, 20: &#39;elephant&#39;, 21: &#39;bear&#39;, 22: &#39;zebra&#39;, 23: &#39;giraffe&#39;, 24: &#39;backpack&#39;, 25: &#39;umbrella&#39;, 26: &#39;handbag&#39;, 27: &#39;tie&#39;, 28: &#39;suitcase&#39;, 29: &#39;frisbee&#39;, 30: &#39;skis&#39;, 31: &#39;snowboard&#39;, 32: &#39;sports ball&#39;, 33: &#39;kite&#39;, 34: &#39;baseball bat&#39;, 35: &#39;baseball glove&#39;, 36: &#39;skateboard&#39;, 37: &#39;surfboard&#39;, 38: &#39;tennis racket&#39;, 39: &#39;bottle&#39;, 40: &#39;wine glass&#39;, 41: &#39;cup&#39;, 42: &#39;fork&#39;, 43: &#39;knife&#39;, 44: &#39;spoon&#39;, 45: &#39;bowl&#39;, 46: &#39;banana&#39;, 47: &#39;apple&#39;, 48: &#39;sandwich&#39;, 49: &#39;orange&#39;, 50: &#39;broccoli&#39;, 51: &#39;carrot&#39;, 52: &#39;hot dog&#39;, 53: &#39;pizza&#39;, 54: &#39;donut&#39;, 55: &#39;cake&#39;, 56: &#39;chair&#39;, 57: &#39;couch&#39;, 58: &#39;potted plant&#39;, 59: &#39;bed&#39;, 60: &#39;dining table&#39;, 61: &#39;toilet&#39;, 62: &#39;tv&#39;, 63: &#39;laptop&#39;, 64: &#39;mouse&#39;, 65: &#39;remote&#39;, 66: &#39;keyboard&#39;, 67: &#39;cell phone&#39;, 68: &#39;microwave&#39;, 69: &#39;oven&#39;, 70: &#39;toaster&#39;, 71: &#39;sink&#39;, 72: &#39;refrigerator&#39;, 73: &#39;book&#39;, 74: &#39;clock&#39;, 75: &#39;vase&#39;, 76: &#39;scissors&#39;, 77: &#39;teddy bear&#39;, 78: &#39;hair drier&#39;, 79: &#39;toothbrush&#39;}
  117. orig_img: array([[[168, 167, 166],
  118. [165, 165, 165],
  119. [165, 166, 167],
  120. ...,
  121. [183, 186, 178],
  122. [183, 186, 178],
  123. [184, 187, 179]],
  124. [[168, 167, 165],
  125. [166, 165, 165],
  126. [166, 167, 166],
  127. ...,
  128. [184, 187, 179],
  129. [183, 186, 178],
  130. [184, 187, 179]],
  131. [[168, 167, 164],
  132. [167, 167, 164],
  133. [167, 167, 165],
  134. ...,
  135. [184, 187, 178],
  136. [184, 187, 179],
  137. [183, 186, 178]],
  138. ...,
  139. [[196, 192, 185],
  140. [196, 192, 185],
  141. [196, 192, 185],
  142. ...,
  143. [ 25, 29, 38],
  144. [ 22, 25, 35],
  145. [ 20, 24, 34]],
  146. [[199, 195, 187],
  147. [197, 193, 186],
  148. [197, 193, 186],
  149. ...,
  150. [ 23, 26, 35],
  151. [ 22, 25, 35],
  152. [ 22, 25, 35]],
  153. [[199, 195, 187],
  154. [199, 195, 187],
  155. [199, 195, 187],
  156. ...,
  157. [ 20, 24, 33],
  158. [ 19, 23, 33],
  159. [ 19, 23, 33]]], dtype=uint8)
  160. ~~~
  161. </details>
  162. # 答案1
  163. **得分**: 0
  164. 愿上帝怜悯那位编写Ultralytics文档的人...以下是如何仅打印标签的方法
  165. ```python
  166. from ultralytics import YOLO
  167. model = YOLO('yolov8n.pt')
  168. results = model('http://images.cocodataset.org/val2017/000000397133.jpg')
  169. print(model.names)
  170. for result in results:
  171. boxes = result.boxes.cpu().numpy()
  172. for box in boxes:
  173. print(model.names[box.cls[0]])

model.names 包含所有可预测的类别。每个 box 都有一个 cls(缩写为 class)属性,它是一个整数值列表。您可以在 model.names 字典中查找该类别。

附注:每个 box 都有一个 整数 列表,表示模型可以为一个边界框返回多个类别。此示例仅获取 box.cls 列表中的第一个类别。

英文:

May God have mercy on whoever wrote Ultralytics's docs... Here's how you can print only the labels:

  1. from ultralytics import YOLO
  2. model = YOLO(&#39;yolov8n.pt&#39;)
  3. results = model(&#39;http://images.cocodataset.org/val2017/000000397133.jpg&#39;)
  4. print(model.names)
  5. for result in results:
  6. boxes = result.boxes.cpu().numpy()
  7. for box in boxes:
  8. print(model.names[box.cls[0]])

model.names contains all the classes that can be predicted. Each box has a cls (class for short) attribute which is a list of int values. You can search for that class in the model.names dictionary.

PS: Each box has a list of ints which indicates that the model can return multiple classes for one bounding box. This example only takes first of the classes in the box.cls list.

huangapple
  • 本文由 发表于 2023年6月22日 01:35:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定