Help us understand the problem. What is going on with this article?

WEBカメラから抜いたフレームをYolov2で解析し、結果ファイルをDropboxへ逐次送信する

More than 1 year has passed since last update.

ひとまずメモ。編集途中。

やったこと

WEBカメラを起動し、フレームを抜いてyolov2にかけて、検知した猫の数をファイルに吐き出したあと、それをDropbox上のあるフォルダにアップロード(すでにあれば上書き)するプログラム。Dropboxへの上書きの更新でAPI使う部分の書き方がよくわからず、時間を食った。動きばいいやの精神なので、まだ荒削り。Dropboxのトークン取得の仕方は割愛。

from darkflow.net.build import TFNet
import cv2
import numpy as np
import time
import datetime
import dropbox

dbx=dropbox.Dropbox('???')#???にはキーを入力
dbx.users_get_current_account()


options = {"model": "cfg/yolo.cfg", "load": "yolov2.weights", "threshold": 0.1}
tfnet = TFNet(options)

# カメラの起動
cap = cv2.VideoCapture(0)

class_names = ['person','aeroplane', 'bicycle', 'bird', 'boat', 'bottle',
              'bus', 'car', 'cat', 'chair', 'cow', 'diningtable',
              'dog', 'horse', 'motorbike', 'pottedplant',
              'sheep', 'sofa', 'train', 'tvmonitor']

num_classes = len(class_names)
class_colors = []
for i in range(0, num_classes):
    hue = 255*i/num_classes
    col = np.zeros((1,1,3)).astype("uint8")
    col[0][0][0] = hue
    col[0][0][1] = 128
    col[0][0][2] = 255
    cvcol = cv2.cvtColor(col, cv2.COLOR_HSV2BGR)
    col = (int(cvcol[0][0][0]), int(cvcol[0][0][1]), int(cvcol[0][0][2]))
    class_colors.append(col)

def main():

    count=0
    start=time.time()

    while(True):

        # 動画ストリームからフレームを取得
        ret, frame = cap.read()
        frame=cv2.resize(frame,(int(frame.shape[1]/4),int(frame.shape[0]/4)))#リサイズ
        result = tfnet.return_predict(frame)#物体検出の実行とその結果をresultへ格納

        count=0#ここに初期化を入れると、1フレームずつの人数になり、ここを消すと累計になる

        for item in result:
            tlx = item['topleft']['x']
            tly = item['topleft']['y']
            brx = item['bottomright']['x']
            bry = item['bottomright']['y']
            label = item['label']
            conf = item['confidence']

            if conf > 0.6:
                class_num=0
                for i in class_names:
                    if label == i:
                        class_num = class_names.index(i)
                        break

                #print('class_num=',class_num)
                if(class_num == 8):
                    count=count+1

                d=datetime.datetime.today()
                print('cat_count=',count,d)#catだった場合、カウントを増やす
                str_count=str(count)
                str_time=str(d)
                f=open('./result/test.txt','a')
                f.write(str_count)
                f.write('\t')
                f.write(str_time)
                f.write('\n')
                f.close()

                #枠の作成
                cv2.rectangle(frame, (tlx, tly), (brx, bry), class_colors[class_num], 2)

                #ラベルの作成
                text = label + " " + ('%.2f' % conf)
                cv2.rectangle(frame, (tlx, tly - 15), (tlx + 100, tly + 5), class_colors[class_num], -1)
                cv2.putText(frame, text, (tlx, tly), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,0), 1)

        # 表示
        cv2.imshow("Show FLAME Image", frame)

        # escを押したら終了。
        k = cv2.waitKey(10);
        if k == 27:  break;
        time.sleep(1)
        process_time=time.time()-start#かかった時間の計算
        print('process_time=',process_time)
        if(process_time>10):
            print('10秒経過')
            f=open('./result/test.txt','rb')
            dbx.files_upload(f.read(),'/result/test.txt',mode=dropbox.files.WriteMode.overwrite)
            f.close()
            start=time.time()

    cap.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    main()
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away