0

I am a junior high school student, I am not living in English-speaking countries. So My english not good. My dad said I can need to asking about coding questions in this community. I know asking questions is very important, Please help me solve this question. I interesting about image processing and I still grope this field. At the same time, I hope you can give me some suggestions for the software engineering.

Error message : Traceback (most recent call last): File "C:/Users/user/Desktop/PythonTrueTest/TrackingTesT1.py", line 88, in ok, frame = cap.read() AttributeError: 'numpy.ndarray' object has no attribute 'read'

import time
import mss
import numpy
from yoloOpencv import opencvYOLO
from PIL import ImageFont, ImageDraw, Image
from urllib.request import urlopen
import cv2
import imutils
tracker = cv2.TrackerMIL_create()

yolo = opencvYOLO(modeltype="yolov3-tiny", \
                  objnames="mask_face_outsource_yolov3-tiny/obj.names", \
                  weights="mask_face_outsource_yolov3-tiny/yolov3-tiny_final.weights", \
                  cfg="mask_face_outsource_yolov3-tiny/yolov3-tiny.cfg")

labels_tw = {"none": "沒有戴好", "good": "正確配戴口罩", "bad": "戴上口罩"}


def printText(bg, txt, color=(0, 255, 0, 0), size=0.7, pos=(0, 0), type="Chinese"):
    (b, g, r, a) = color

    if (type == "English"):
        cv2.putText(bg, txt, pos, cv2.FONT_HERSHEY_SIMPLEX, size, (b, g, r, a), 2, cv2.LINE_AA)

    else:
        ## Use simsum.ttf to write Chinese.
        fontpath = "fonts/wt009.ttf"
        font = ImageFont.truetype(fontpath, int(size * 10 * 4))
        img_pil = Image.fromarray(bg)

        draw = ImageDraw.Draw(img_pil)
        draw.text(pos, txt, font=font, fill=(b, g, r, a))
        bg = numpy.array(img_pil)
    return bg

with mss.mss() as sct:
    # Part of the screen to capture
    monitor = {"top": 40, "left": 0, "width": 800, "height": 640}
    write_video = True
    while "Screen capturing":
        last_time = time.time()
        #print(pyautogui.position())
        # Get raw pixels from the screen, save it to a Numpy array

        img = numpy.array(sct.grab(monitor))
        cap = img
        img = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB)
        fps = (1 / (time.time() - last_time))

        print("fps: {}".format(1 / (time.time() - last_time)))
        # Display the picture

        yolo.getObject(img, labelWant="", drawBox=True, bold=2, textsize=0.95, bcolor=(0, 0, 255),
                       tcolor=(255, 255, 255))

        for id, label in enumerate(yolo.labelNames):
            x = yolo.bbox[id][0]
            y = yolo.bbox[id][1]
            w = yolo.bbox[id][2]
            h = yolo.bbox[id][3]
            cx = int(x)
            #if (cx > width): cx = width - 60
            cy = int(y - h / 3)
            if (cy < 0): cy = 0
            if (label == "bad"):
                txt_color = (0, 0, 255, 0)
            elif (label == "none"):
                txt_color = (255, 255, 0, 0)
            else:
                txt_color = (0, 255, 0, 0)

            txt_size = round(w / 250, 1)
            print(labels_tw[label], (w, h))
            img = printText(bg=img, txt=labels_tw[label], color=txt_color, size=txt_size, pos=(cx, cy), type="Chinese")
        cv2.putText(img, "FPS : " + str(int(fps)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2)
        cv2.imshow("Frame", img)

        k = cv2.waitKey(1)

        k = cv2.waitKey(30) & 0xff
        if k == 27:
            break
bbox = cv2.selectROI(img, False)
ok = tracker.init(img, bbox)
cv2.destroyWindow("ROI selector")
while True:
    ok, frame = cap.read()
    ok, bbox = tracker.update(frame)

    if ok:
        p1 = (int(bbox[0]), int(bbox[1]))
        p2 = (int(bbox[0] + bbox[2]),
              int(bbox[1] + bbox[3]))
        cv2.rectangle(frame, p1, p2, (0, 0, 255), 2, 2)
        print(bbox[0])

    cv2.imshow("Tracking", frame)
    k = cv2.waitKey(1) & 0xff
    if k == 27: break`
YY Z
  • 1
  • Welcome! Did you try to debug it yourself, first? – jasie Apr 11 '22 at 14:50
  • Yes , I have try my best to debug and searching about this error of sloving problem , but I just coding few week , I think this website [link](https://stackoverflow.com/questions/22906394/numpy-ndarray-object-has-no-attribute-read) is nearly my problem , but I don't know how to change my code , This website is about image but My code is about video tracking. My coding ability is beginner,I still want to solving my problem ,but I don't know how to start. – YY Z Apr 13 '22 at 11:11
  • My english is not good , so please bear with me . – YY Z Apr 13 '22 at 11:13
  • There are multiple SO question with the same error, e.g. https://stackoverflow.com/questions/11685936/why-am-i-getting-attributeerror-object-has-no-attribute. start by goind through all of them. Furthermore, there a dozens of articles in the internet regarding that error, e.g. https://www.geeksforgeeks.org/python-attributeerror/. Read them und learn to understand the error and its cause. – jasie Apr 13 '22 at 13:08
  • I can't figure out the problem and how to fix it. I tried my best to read two websit & modify my code, but it's getting worse and worse, I hope you can give me some tips – YY Z Apr 17 '22 at 08:00

0 Answers0