iT邦幫忙

0

Raspberry Pi 用 yolo + OpenCV ??? 有 person 時回傳 ture 的作法

  • 分享至 

  • xImage

家中有台 Raspberry Pi,想用這個Pi 來作 smartcam, trigger 一定的條件後,可以寄通知到 line notify.
之前是用Pir sensor 來 trigger , 但看了openCV 和 AI 的書後,覺得一個鏡頭就能搞定。

按書的指示裝了openCV, 上了yolo 的 py code 檔後,發現他會在screen 上有個monitor而且會框出來拍到的物件。

我想把trigger 這些框的條件抓出來,另外作trigger 的event, 試了後發現應是 classes這個變數,但他會tuple 和array 變來變去,所以用個參數強制變成list, 再去接他前2 個參數。 (因為有人時會出現 [0 )

   boxx = list(classes)
        while True:        
            if boxx:
                print (str(classes)[0:2])
            
                if str(classes)[0:2] == '[0':
                    print ( str(classes) + '抓到了') 
                    cv2.imwrite( "/home/pi/Pictures/yolo" + str(i)+'.jpeg', frame)
                    time.sleep(1)
                else:
                    print ( str(classes) +  'alient')
                    time.sleep(1)
            else:
                print ('non')
                time.sleep(1)

想請問熟悉open CV 和 yolo 的高手,有更好的方式嗎?

以下是書上裁錄的code , 我刪了部份(...的地方),以免有著作權,下半部是我自己亂加的,但不太work.


import ...

def initNet():
    CONFIG = 'yolov4-tiny.cfg'
    WEIGHT = 'yolov4-tiny.weights'
    NAMES = 'coco.names'``
…
    net = cv2.dnn.readNet(CONFIG, WEIGHT)
    model = cv2.dnn_DetectionModel(net)
    model.setInputParams(size=(416, 416), scale=1/255.0)
…
    return model, names, colors
    
def nnPro…..(image, model):
    classes, confs, boxes = model.detect(image, 0.6, 0.3)
    return classes, confs, boxes

def drawBox(image, classes, confs, boxes, names, colors):
    new_image = image.copy()
…
        cv2.rectangle(new_image, (x, y), (x + w, y + h), color, 2)
        cv2.putText(new_image, label, (x, y - 10), 
            cv2.FONT_HERSHEY_SIMPLEX, 0.7, color, 2
        )
    return new_image

model, names, colors = initNet()
cap = cv2.VideoCapture(0)
….


while True:
    begin_time = time.time()
    ret, frame = cap.read()
    frame = cv2.resize(frame, (WIDTH, HEIGHT))
    
    classes, confs, boxes = nnProcess(frame, model)
    frame = drawBox(frame, classes, confs, boxes, names, colors)

    fps = 'fps: {:.2f}'.format(1 / (time.time() - begin_time))
    cv2.putText(frame, fps, (10, 30), 
        cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 204, 255), 2
    )

    cv2.imshow('video', frame)
    
    boxx = list(classes)
        while True:        
            if boxx:
                print (str(classes)[0:2])
            
                if str(classes)[0:2] == '[0':
                    print ( str(classes) + '抓到了') 
                    cv2.imwrite( "/home/pi/Pictures/yolo" + str(i)+'.jpeg', frame)
                    time.sleep(1)
                else:
                    print ( str(classes) +  'alient')
                    time.sleep(1)
            else:
                print ('non')
                time.sleep(1)
      

    if cv2.waitKey(1) == 27:
...
        break
    
froce iT邦大師 1 級 ‧ 2022-06-22 09:02:45 檢舉
能框,你有能力改code就有能力傳回值啊...
bill0704 iT邦新手 5 級 ‧ 2022-06-23 12:30:20 檢舉
可以問是哪本書嗎? 推嗎XD??

另外我想不太work的原因會不會因為是你有兩個while迴圈,且第二個沒有寫跳出的條件,而且縮排也不太對

如要發問可以上傳跑過程的錯誤資訊,會比較好抓bug
後來有聯絡作者,他超佛心的,有回答是用 label 而且書上就有提到,只是我沒看出來。哈,因為真的不太懂。書名是AIOT與OpenCV實戰應用,書上的東西跟著作都能作出來,很推薦。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
haward79
iT邦研究生 2 級 ‧ 2022-06-22 09:17:17
最佳解答

你提到的功能理論上是可以實做出來的、沒問題

只是如果你是程式小白,建議你一步一步來
將整個系統拆分成數個模組再個別實做
實做過程遇到問題再一一突破!

如果直接從頭問到尾,但卻沒有實做經驗
容易產生「不合理的問題」
因為所謂的問題,是被想像出來的
而不是實際會面對到的!!

我要發表回答

立即登入回答