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`