Skip to content

'NoneType' object has no attribute 'shape' #290

Open

Description

@sanadsad75

I‘m running the real_time_object_detection program, and the problem that i faced is : Traceback (most recent call last):
File "C:\Users\SANAD SALEH\Desktop\yolo-master\yolo.py", line 38, in
img = imutils.resize(img, width=960 ,height=1800)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\SANAD SALEH\AppData\Roaming\Python\Python312\site-packages\imutils\convenience.py", line 69, in resize
(h, w) = image.shape[:2]
^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'shape'

Process finished with exit code 1

while my code that im working at it is : `from ultralytics import YOLO
import cv2
import math
import requests
import cv2
import numpy as np
import imutils
from classess import classNames
import playsound as ps
import threading
import time

model = YOLO('yolov8n.pt')

x = classNames
font = cv2.FONT_HERSHEY_SIMPLEX

insert your url from the ip webcam app below

url = "http://192.168.0.103:8080"
audioplaying = False
def play():
global audioplaying
if (audioplaying == False):
audioplaying = True
# enter the location of your audio file below
ps.playsound(r"C:\Users\SANAD SALEH\Desktop\yolo-master\audio\HHumanDetectedER.mp3")
audioplaying = False

counter = 0
firsttime = True
cap = cv2.VideoCapture(0)
t1 = time.time()
while True:
img_resp = requests.get(url)
img_arr = np.array(bytearray(img_resp.content), dtype=np.uint8)
img = cv2.imdecode(img_arr, -1)
img = imutils.resize(img, width=960 ,height=1800)
result = model(img, stream=True)
for r in result:
boxes = r.boxes
for box in boxes:
x1, y1, x2, y2 = box.xyxy[0]
x1, y1, x2, y2 = int(x1),int(y1),int(x2),int(y2)
cv2.rectangle(img,(x1,y1),(x2,y2),(0,0,255),3)
conf = math.ceil((box.conf[0]*100))/100
cls = int(box.cls[0])
cv2.putText(img,f'{x[cls]}{conf}',(x1,y1),font,1,(0,255,0))

        if cls == 0:
         counter += 1
         if counter == 2:
           t = threading.Thread(target=play)
           t.start()
           counter += 1
         if counter == 12:
            t = threading.Thread(target=play)
            t.start()
            counter = 0 
                    
cv2.imshow('me',img)
if cv2.waitKey(1) & 0xFF == 27:
    break

cap.release()
cv2.destroyAllWindows()
`
how can i solve this problem ?

Activity

  1. gauritomar commented on Feb 16, 2024

    @gauritomar

    Add this to your code

    while True:
        img_resp = requests.get(url)
        if img_resp.status_code != 200:
            print("Failed to retrieve image from URL")
            continue
        
        img_arr = np.array(bytearray(img_resp.content), dtype=np.uint8)
        img = cv2.imdecode(img_arr, -1)
        
        if img is None:
            print("Failed to decode image")
            continue
        
        img = imutils.resize(img, width=960, height=1800)
        result = model(img, stream=True)
        # Rest of your code...
    

    to check is the error is in retrieving the image using requests or decoding it.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions