PyImageSearch / imutils

A series of convenience functions to make basic image processing operations such as translation, rotation, resizing, skeletonization, and displaying Matplotlib images easier with OpenCV and Python.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

'NoneType' object has no attribute 'shape'

sanadsad75 opened this issue · comments

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 ?

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.