hamza
hamza

Reputation: 11

TypeError: fromarray() missing 1 required positional argument: 'obj'

I have been asking questions on StackOverflow but have not been getting any positive responses. Got banned multiple times from posting further questions. Please provide help this time.

I want to perform custom object detection using custom trained YOLOv5 model on a real-time video feed taken from the webcam. I am using Django for this purpose (later on I will connect this with React.js frontend). I have been successful with accessing the webcam feed using Django but when I try to run my custom yolov5 model on the video feed. I am getting this error and there is no video showing up in index.html.

[12/Jun/2022 02:43:03] "GET / HTTP/1.1" 200 329
Traceback (most recent call last):
  File "c:\users\mh_s1\appdata\local\programs\python\python37\lib\wsgiref\handlers.py", line 138, in run
    self.finish_response()
  File "c:\users\mh_s1\appdata\local\programs\python\python37\lib\wsgiref\handlers.py", line 183, in finish_response
    for data in self.result:
  File "D:\University\FYP\FYP-CovidDefence\stream\webcam\views.py", line 42, in stream
    data=im.fromarray()
TypeError: fromarray() missing 1 required positional argument: 'obj'

Here is my views.py file code:

from django.http import StreamingHttpResponse
import cv2
from PIL import Image as im
import yolov5
from yolov5.utils.general import (check_img_size, non_max_suppression, scale_coords, 
                                  check_imshow, xyxy2xywh, increment_path)
from yolov5.utils.torch_utils import select_device, time_sync
from yolov5.utils.plots import Annotator, colors

import io

import torch


# Create your views here.
def index(request):
    return render(request,'index.html')

# Change this to the correct path
path_hubconfig = (r"D:\University\FYP\FYP-CovidDefence\stream\ObjectDetection\yolov5")
path_weightfile = (r"D:\University\FYP\FYP-CovidDefence\stream\ObjectDetection\yolov5\best.pt")  # or any custom trained model

model = torch.hub.load(path_hubconfig, 'custom', path=path_weightfile, source='local')


def stream():
    cap = cv2.VideoCapture(0)
    while True:
        ret, frame = cap.read()
        if not ret:
            print("Error: failed to capture image")
            break
        results = model(frame, augment=True)
        # process
        #annotator = Annotator(frame, line_width=2, pil=not ascii)
        for i in results.render():
            data=im.fromarray()
            data.save('demo.jpg')
        #cv2.imwrite('demo.jpg',frame)
        #print(results)    
 
        image_bytes = cv2.imencode('.jpg', frame)[1].tobytes()
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + open('demo.jpg', 'rb').read() + b'\r\n')

def video_feed(request):
    return StreamingHttpResponse(stream(), content_type='multipart/x-mixed-replace; boundary=frame') 

Upvotes: 0

Views: 408

Answers (1)

hamza
hamza

Reputation: 11

I figured it out. I actually forgot to pass 'i' to fromarray()

for i in results.render():
    data=im.fromarray(i)

Upvotes: 1

Related Questions