# **影像辨識** --- [TOC] --- 環境 : YOLOv4安裝環境 === Installing Python(3.7.6) --- *注意版本問題 版本會影響到後續的發展 https://www.python.org/downloads/windows/ ![](https://i.imgur.com/l4zTEUR.png) 因為我截圖前就先下載了 ![](https://i.imgur.com/Ii0jSdT.png) 但一開始下載的結果應該是這樣 ![](https://i.imgur.com/3uUDnl9.png) --- Git Installation (2.26.2) --- ![](https://i.imgur.com/n2i8zoE.png) --- CMAKE Installation(3.18.0) --- *它是可以進行編譯的工具 下載 cmake-3.18.0-rc3-win64-x64 https://cmake.org/download/ --- Visual Studio Installation(2019) --- *寫codeing可以用 下載 2019 版本 https://visualstudio.microsoft.com/zh-hant/downloads/ --- Updating GPU Drivers --- ![](https://i.imgur.com/e9dPUoE.png) --- CUDA Installation(10.2) --- ![](https://i.imgur.com/DACEK0F.png) --- CUDnn Installation(7.6.5) --- ![](https://i.imgur.com/YTOIag9.png) --- Open CV Installation (4.1.0) --- https://opencv.org/opencv-4-1/ ![](https://i.imgur.com/oeClrsy.png) 下載 https://github.com/opencv/opencv_contrib 新增 ”build” 資料夾 ![](https://i.imgur.com/69KhOGp.png) --- CMake --- 打開 CMake ![](https://i.imgur.com/PBWPAZ0.png) 按下方的 Configure 後 ![](https://i.imgur.com/xvjL3c1.png) BUILD_opencv_world 要打勾 ![](https://i.imgur.com/sNeQ15K.png) 按下方的 Generate 跑完後出現這兩行就代表成功 ![](https://i.imgur.com/dWx4dAB.png) --- Building OpenCV in Visual Studio --- 打開 ALL_BUILD.vcxproj ![](https://i.imgur.com/HV7Sw22.png) 這兩個檔案要建置 ![](https://i.imgur.com/1E0xEn5.png) ![](https://i.imgur.com/n7Uuv2o.png) 建置成功後,測試OpenCV ![](https://i.imgur.com/mwO4XX3.png) 沒有出現錯誤訊息,代表成功。 --- Downloading DarkNet --- https://github.com/AlexeyAB/darknet ![](https://i.imgur.com/opimtyM.png) 複製 C:\opencv\build\bin\Release 裡的 ![](https://i.imgur.com/zg3tI8u.png) ![](https://i.imgur.com/rc6Rgor.png) 貼到 C:\Yolo_v4\darknet\build\darknet\x64 裡 --- Copying Open files into Darknet --- 複製 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2\bin 裡的 ![](https://i.imgur.com/rLzLIkz.png) 貼到 C:\Yolo_v4\darknet\build\darknet\x64 裡 --- Changing the CuDNN version in Darknet --- 編輯 C:\Yolo_v4\darknet\build\darknet 裡的 ![](https://i.imgur.com/oY2db4J.png) ![](https://i.imgur.com/Co38kPj.png) 把 CUDA 10.0 改成 CUDA 10.2 ![](https://i.imgur.com/ETAGpj3.png) --- Compile YOLOv4 with updated CUDA version --- 打開 ![](https://i.imgur.com/ZaOYkLP.png) 並建置 上方記得調整成 ![](https://i.imgur.com/1E0xEn5.png) --- Compiling Darknet --- 打開 ![](https://i.imgur.com/k5Vtx4z.png) ![](https://i.imgur.com/1E0xEn5.png) 檔案按右鍵,屬性 新增 C:\opencv\build\install\include ![](https://i.imgur.com/0RtWxdt.png) 移除 CUDNN_HALF ![](https://i.imgur.com/PGzANmQ.png) 移除 compute_75,sm_75 ![](https://i.imgur.com/ynsk6w5.png) 新增 C:\opencv\build\install\x64\vc16\lib ![](https://i.imgur.com/cDwQB15.png) --- Run YOLOv4 on Webcam --- ![](https://i.imgur.com/Q0yiwyb.png) darknet_video_mod.py 利用現成程式碼做測試 ```gherkin= from ctypes import * # Import libraries import math import random import os import cv2 import numpy as np import time import darknet def convertBack(x, y, w, h): xmin = int(round(x - (w / 2))) xmax = int(round(x + (w / 2))) ymin = int(round(y - (h / 2))) ymax = int(round(y + (h / 2))) return xmin, ymin, xmax, ymax def cvDrawBoxes(detections, img): # Colored labels dictionary color_dict = { 'person' : [0, 255, 255], 'bicycle': [238, 123, 158], 'car' : [24, 245, 217], 'motorbike' : [224, 119, 227], 'aeroplane' : [154, 52, 104], 'bus' : [179, 50, 247], 'train' : [180, 164, 5], 'truck' : [82, 42, 106], 'boat' : [201, 25, 52], 'traffic light' : [62, 17, 209], 'fire hydrant' : [60, 68, 169], 'stop sign' : [199, 113, 167], 'parking meter' : [19, 71, 68], 'bench' : [161, 83, 182], 'bird' : [75, 6, 145], 'cat' : [100, 64, 151], 'dog' : [156, 116, 171], 'horse' : [88, 9, 123], 'sheep' : [181, 86, 222], 'cow' : [116, 238, 87],'elephant' : [74, 90, 143], 'bear' : [249, 157, 47], 'zebra' : [26, 101, 131], 'giraffe' : [195, 130, 181], 'backpack' : [242, 52, 233], 'umbrella' : [131, 11, 189], 'handbag' : [221, 229, 176], 'tie' : [193, 56, 44], 'suitcase' : [139, 53, 137], 'frisbee' : [102, 208, 40], 'skis' : [61, 50, 7], 'snowboard' : [65, 82, 186], 'sports ball' : [65, 82, 186], 'kite' : [153, 254, 81],'baseball bat' : [233, 80, 195],'baseball glove' : [165, 179, 213],'skateboard' : [57, 65, 211], 'surfboard' : [98, 255, 164],'tennis racket' : [205, 219, 146],'bottle' : [140, 138, 172],'wine glass' : [23, 53, 119], 'cup' : [102, 215, 88],'fork' : [198, 204, 245],'knife' : [183, 132, 233],'spoon' : [14, 87, 125], 'bowl' : [221, 43, 104],'banana' : [181, 215, 6],'apple' : [16, 139, 183],'sandwich' : [150, 136, 166],'orange' : [219, 144, 1], 'broccoli' : [123, 226, 195],'carrot' : [230, 45, 209],'hot dog' : [252, 215, 56],'pizza' : [234, 170, 131], 'donut' : [36, 208, 234],'cake' : [19, 24, 2],'chair' : [115, 184, 234],'sofa' : [125, 238, 12], 'pottedplant' : [57, 226, 76],'bed' : [77, 31, 134],'diningtable' : [208, 202, 204],'toilet' : [208, 202, 204], 'tvmonitor' : [208, 202, 204],'laptop' : [159, 149, 163],'mouse' : [148, 148, 87],'remote' : [171, 107, 183], 'keyboard' : [33, 154, 135],'cell phone' : [206, 209, 108],'microwave' : [206, 209, 108],'oven' : [97, 246, 15], 'toaster' : [147, 140, 184],'sink' : [157, 58, 24],'refrigerator' : [117, 145, 137],'book' : [155, 129, 244], 'clock' : [53, 61, 6],'vase' : [145, 75, 152],'scissors' : [8, 140, 38],'teddy bear' : [37, 61, 220], 'hair drier' : [129, 12, 229],'toothbrush' : [11, 126, 158] } for detection in detections: x, y, w, h = detection[2][0],\ detection[2][1],\ detection[2][2],\ detection[2][3] name_tag = str(detection[0].decode()) for name_key, color_val in color_dict.items(): if name_key == name_tag: color = color_val xmin, ymin, xmax, ymax = convertBack( float(x), float(y), float(w), float(h)) pt1 = (xmin, ymin) pt2 = (xmax, ymax) cv2.rectangle(img, pt1, pt2, color, 1) cv2.putText(img, detection[0].decode() + " [" + str(round(detection[1] * 100, 2)) + "]", (pt1[0], pt1[1] - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2) return img netMain = None metaMain = None altNames = None def YOLO(): global metaMain, netMain, altNames configPath = "./cfg/yolov4.cfg" # Path to cfg weightPath = "./yolov4.weights" # Path to weights metaPath = "./cfg/coco.data" # Path to meta data if not os.path.exists(configPath): # 檢查文件是否存在,否則 return ValueError raise ValueError("Invalid config path `" + os.path.abspath(configPath)+"`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath)+"`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath)+"`") if netMain is None: # 檢查 metaMain, NetMain 和 altNames. Loads it in script netMain = darknet.load_net_custom(configPath.encode( "ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass cap = cv2.VideoCapture(0) # 取消使用 Webcam #cap = cv2.VideoCapture("test2.mp4") # Local Stored video detection - Set input video frame_width = int(cap.get(3)) # Returns the width and height of capture video frame_height = int(cap.get(4)) # Set out for video writer out = cv2.VideoWriter( # Set the Output path for video writer "./Demo/output.avi", cv2.VideoWriter_fourcc(*"MJPG"), 10.0, (frame_width, frame_height)) print("Starting the YOLO loop...") # Create an image we reuse for each detect darknet_image = darknet.make_image(frame_width, frame_height, 3) # Create image according darknet for compatibility of network while True: # Load the input frame and write output frame. prev_time = time.time() ret, frame_read = cap.read() # Capture frame and return true if frame present # For Assertion Failed Error in OpenCV if not ret: # Check if frame present otherwise he break the while loop break frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) # Convert frame into RGB from BGR and resize accordingly frame_resized = cv2.resize(frame_rgb, (frame_width, frame_height), interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image,frame_resized.tobytes()) # Copy that frame bytes to darknet_image detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25) # Detection occurs at this line and return detections, for customize we can change the threshold. image = cvDrawBoxes(detections, frame_resized) # Call the function cvDrawBoxes() for colored bounding box per class image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) print(1/(time.time()-prev_time)) cv2.imshow('Demo', image) # Display Image window cv2.waitKey(3) out.write(image) # Write that frame into output video cap.release() # For releasing cap and out. out.release() print(":::Video Write Completed") if __name__ == "__main__": YOLO() # Calls the main function YOLO() ``` ###### tags: `By 田于昕` `By 劉芳杏` --- 訓練 : YOLOv4訓練臉部辨識 === 下載 yolov4.conv.137 --- https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.conv.137 下載在 ==D:\train\darknet\build\darknet\x64== --- yolov4-custom.cfg --> yolov4-test.cfg --- 複製 ==D:\train\darknet\build\darknet\x64\cfg\yolov4-custom.cfg== 另存為 ==D:\train\darknet\build\darknet\x64\cfg\yolov4-test.cfg== ![](https://i.imgur.com/vApvsEF.png) --- 修改 yolov4-test.cfg --- 修改以下7個地方: - batch改為32(原本改64,但出現CUDA記憶體不足的錯誤訊息) - subdivisions改為64 - width和height都改為416 - max_batchs改為Max((分類項數*2000),6000) 一開始,我只有三個Class,所以分類項數=3 所以max_batchs=Max(6000,6000)=6000 (官方寫是不要少於6000次的模擬) - steps輸入兩個數字,分別為max_batchs的80%以及90% 所以我這裡會輸入4800,5400 ![](https://i.imgur.com/Gzhuxvz.png) - 按下Ctrl+F搜尋 [yolo],classes和filters共有三個地方需修改 一、 下方會出現classes,將它改成3 二、 其上方的[convolutional]之下,把filters改成(classes+5)*3=24 ![](https://i.imgur.com/8ZjgLAt.png) --- 建立test.names --- 在 ==D:\train\darknet\build\darknet\x64\data== 建立 test.names 裡面紀錄classes的名稱(Label) ![](https://i.imgur.com/2TNh6lb.png) --- 建立test.data,並修改 --- 在 ==D:\train\darknet\build\darknet\x64\data== 建立 test.data 裡面紀錄classes數量、train檔案路徑檔、valid檔案路徑檔、names檔、backup位置 ![](https://i.imgur.com/xMeJn7l.png) --- 建立資料夾(obj) --- 在 ==D:\train\darknet\build\darknet\x64\data== 下建立一個資料夾 obj 在 obj 下建立資料夾 test,然後在 test 下丟入照片(399張) 副檔名都統一用*.jpg ![](https://i.imgur.com/cFBfs44.png) --- 建立資料夾(valid) --- 在 ==D:\train\darknet\build\darknet\x64\data== 下建立資料夾 valid 在valid之下建立資料夾test,在裡面丟幾張跟obj/test不同的照片 ![](https://i.imgur.com/XUdRCzj.png) --- 建立的空白文字檔(test_train.txt) --- 在 ==D:\train\darknet\build\darknet\x64\data== 建立的空白文字檔 test_train.txt 將剛剛建立的399張照片的路徑都記錄在裡面 ![](https://i.imgur.com/JCEtWV3.png) (同樣的動作在 test_valid.txt 做一遍,紀錄放在 valid/test 下的另外驗證照片路徑) --- 建立標籤位置(LabelImg) --- 採用LabelImg (Windows_v1.8.0) https://tzutalin.github.io/labelImg/ 解壓縮後放在 ==D:\train\LabelImg\windows_v1.8.0== 下 ![](https://i.imgur.com/fmHJ2xg.png) 先打開檔案下的 data 資料夾,可以看到 predefined_classes.txt 這個檔案,此檔案裡面原始有15個預設標籤 先複製 predefined_classes.txt 並改名為predefined_classes_origin.txt ![](https://i.imgur.com/mSY5hFk.png) 再將原本的predefined_classes.txt的內容改成與 ==D:\train\darknet\build\darknet\x64\data\test.names== 一致 ![](https://i.imgur.com/FwFCygQ.png) 開啟 LabelImg.exe - 點擊Open Dir,選擇到 ==D:\train\darknet\build\darknet\x64\data\obj\test== - 點擊Change Save Dir,選擇同一路徑 ==D:\train\darknet\build\darknet\x64\data\obj\test== - 從Menu-->View-->Auto Saving勾起來 - 在PascalVOC上點一下,會變成YOLO,這樣才會符合yolo的定位格式 - 點擊Create RectBox(快捷鍵是W,需切換至英打,快捷鍵才有效果) ![](https://i.imgur.com/J6dqnzU.png) 去該路徑下看是否有生成檔案,如圖表示生成成功 ![](https://i.imgur.com/5tBqVkz.png) --- 開始訓練 --- cmd到 ==D:\train\darknet\build\darknet\x64== (darknet.exe所在路徑之下) 執行 :::info darknet.exe detector train data/test.data cfg/yolov4-test.cfg yolov4.conv.137 ::: ![](https://i.imgur.com/9Ih9jtf.png) --- 中斷訓練 --- 對cmd按下==Ctrl+C==中斷 可以到 test.data 中指定的 backup 路徑下找到 weights 檔案 ==D:\train\darknet\build\darknet\x64\backup== ![](https://i.imgur.com/dzt2Kdn.png) --- 中斷訓練後欲重新訓練 --- 把train指令改成 :::info darknet.exe detector train data/test.data cfg/yolov4-test.cfg backup/yolov4-test_last.weights ::: 將原本的 yolov4.conv.137 改成 backup/yolov4-test_last.weights 即可 想從1000重新訓練,就改成 backup/yolov4-test_1000.weights 即可 --- 測試 : 訓練結果 === cmd到 ==D:\train\darknet\build\darknet\x64== 下執行 圖片測試 --- :::info darknet.exe detector test data/test.data cfg/yolov4-test.cfg backup/yolov4-test_last.weights ::: 影片測試 --- :::info darknet.exe detector demo data/test.data cfg/yolov4-test.cfg yolov4-test_last.weights -ext_output test.mp4 ::: Webcam測試 --- :::info darknet.exe detector demo data/test.data cfg/yolov4-test.cfg yolov4-test_last.weights -c 0 ::: 最後程式碼 === ```gherkin= from ctypes import * import math import random import os import cv2 import numpy as np import time import darknet import json import datetime import firebase_admin from firebase_admin import credentials from firebase_admin import firestore sent = ['non','non','non'] def convertBack(x, y, w, h): xmin = int(round(x - (w / 2))) xmax = int(round(x + (w / 2))) ymin = int(round(y - (h / 2))) ymax = int(round(y + (h / 2))) return xmin, ymin, xmax, ymax def cvDrawBoxes(detections, img): add = [] point1 = [] point2 = [] for detection in detections: postures = ['Standing', 'Sitting', 'Lying down'] people = ['Tian', 'Jennifer', 'Jenny'] x, y, w, h = detection[2][0],\ detection[2][1],\ detection[2][2],\ detection[2][3] xmin, ymin, xmax, ymax = convertBack( float(x), float(y), float(w), float(h)) pt1 = (xmin, ymin) pt2 = (xmax, ymax) pt3 = ((xmin+xmax)/2),((ymin+ymax)/2) point = [pt3] cv2.rectangle(img, pt1, pt2, (0, 255, 0), 1) cv2.putText(img, detection[0].decode() + " [" + str(round(detection[1] * 100, 2)) + "]", (pt1[0], pt1[1] - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, [0, 255, 0], 2) #localtime = time.asctime( time.localtime(time.time()) ) nowTime = int(time.time()) # 取得現在時間 struct_time = time.localtime(nowTime) # 轉換成時間元組 timeString = time.strftime('%Y-%m-%d %H:%M:%S',struct_time) # 將時間元組轉換成想要的字串 #timeString = "2020-09-09 19:00:00" # 時間格式為字串 time1 = time.strptime(timeString,'%Y-%m-%d %H:%M:%S') # 轉成時間元組 time_stamp = str(time.mktime(time1)) # 轉成時間戳 #print(time_stamp) if (detection[0].decode()) == "Standing" : posture0 = postures.pop(0) add.append(posture0) elif (detection[0].decode()) == "Sitting" : posture1 = postures.pop(1) add.append(posture1) elif (detection[0].decode()) == "Lying down" : posture2 = postures.pop(2) add.append(posture2) elif (detection[0].decode()) == "Tian" : person0 = people.pop(0) add.append(person0) elif (detection[0].decode()) == "Jennifer" : person1 = people.pop(1) add.append(person1) elif (detection[0].decode()) == "Jenny" : person2 = people.pop(2) add.append(person2) if len(point1) == 0 : p1 = point.pop(0) point1.append(p1) elif len(point1) == 1: p2 = point.pop(0) point2.append(p2) ps1 = np.asarray(point1) ps2 = np.asarray(point2) if len(add) == 2 : #print(point1) #print(point2) #print('Object: ',add) a=(ps1-ps2)**2 sum = 0 for d in a[0]: sum += d b=sum**(1/2) #print(b) #print('座標: ',a) #print('未開根號:{:.2f},距離:{:.2f}'.format(sum, sum**(1/2))) sub = ps1[0] - ps2[0] x = abs(sub[0]) #print(x) #print(x) if (b<=210.0)&(b>=40.0)&(x<=47.0)&(x>=0.0): data1 = {'Object': add} data = {'Time': time_stamp ,'Name': add[0],'Pose': add[1]} #print('Object: ',add) if sent[0] == 'non' : if data1['Object'] == ['Tian','Standing'] : sent[0] = 'Standing' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("Tian+pose") collection_ref.add(data) print("已上傳") elif data1['Object'] == ['Tian','Sitting'] : sent[0] = 'Sitting' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("Tian+pose") collection_ref.add(data) print("已上傳") elif data1['Object'] == ['Tian','Lying down'] : sent[0] = 'Lying down' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("Tian+pose") collection_ref.add(data) print("已上傳") elif sent[0] != 'non' : #print(sent) if sent[0] == 'Standing' : if data1['Object'] == ['Tian','Sitting'] : sent[0] = 'Sitting' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("Tian+pose") collection_ref.add(data) print("已上傳") elif data1['Object'] == ['Tian','Lying down'] : sent[0] = 'Lying down' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("Tian+pose") collection_ref.add(data) print("已上傳") elif sent[0] == 'Sitting' : if data1['Object'] == ['Tian','Standing'] : sent[0] = 'Standing' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("Tian+pose") collection_ref.add(data) print("已上傳") elif data1['Object'] == ['Tian','Lying down'] : sent[0] = 'Lying down' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("Tian+pose") collection_ref.add(data) print("已上傳") elif sent[0] == 'Lying down' : if data1['Object'] == ['Tian','Sitting'] : sent[0] = 'Sitting' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("Tian+pose") collection_ref.add(data) print("已上傳") elif data1['Object'] == ['Tian','Standing'] : sent[0] = 'Standing' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("Tian+pose") collection_ref.add(data) print("已上傳") if sent[1] == 'non' : if data1['Object'] == ['Jennifer','Standing'] : sent[1] = 'Standing' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("J+pose") collection_ref.add(data) print("已上傳") elif data1['Object'] == ['Jennifer','Sitting'] : sent[1] = 'Sitting' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("J+pose") collection_ref.add(data) print("已上傳") elif data1['Object'] == ['Jennifer','Lying down'] : sent[1] = 'Lying down' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("J+pose") collection_ref.add(data) print("已上傳") elif sent[1] != 'non' : #print(sent) if sent[1] == 'Standing' : if data1['Object'] == ['Jennifer','Sitting'] : sent[1] = 'Sitting' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("J+pose") collection_ref.add(data) print("已上傳") elif data1['Object'] == ['Jennifer','Lying down'] : sent[1] = 'Lying down' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("J+pose") collection_ref.add(data) print("已上傳") elif sent[1] == 'Sitting' : if data1['Object'] == ['Jennifer','Standing'] : sent[1] = 'Standing' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("J+pose") collection_ref.add(data) print("已上傳") elif data1['Object'] == ['Jennifer','Lying down'] : sent[1] = 'Lying down' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("J+pose") collection_ref.add(data) print("已上傳") elif sent[1] == 'Lying down' : if data1['Object'] == ['Jennifer','Sitting'] : sent[1] = 'Sitting' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("J+pose") collection_ref.add(data) print("已上傳") elif data1['Object'] == ['Jennifer','Standing'] : sent[1] = 'Standing' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("J+pose") collection_ref.add(data) print("已上傳") if sent[2] == 'non' : if data1['Object'] == ['Jenny','Standing'] : sent[2] = 'Standing' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("1d16e844-d471-4911-95eb-554c14e7976ep") collection_ref.add(data) print("已上傳") elif data1['Object'] == ['Jenny','Sitting'] : sent[2] = 'Sitting' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("1d16e844-d471-4911-95eb-554c14e7976ep") collection_ref.add(data) print("已上傳") elif data1['Object'] == ['Jenny','Lying down'] : sent[2] = 'Lying down' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("1d16e844-d471-4911-95eb-554c14e7976ep") collection_ref.add(data) print("已上傳") elif sent[2] != 'non' : #print(sent) if sent[2] == 'Standing' : if data1['Object'] == ['Jenny','Sitting'] : sent[2] = 'Sitting' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("1d16e844-d471-4911-95eb-554c14e7976ep") collection_ref.add(data) print("已上傳") elif data1['Object'] == ['Jenny','Lying down'] : sent[2] = 'Lying down' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("1d16e844-d471-4911-95eb-554c14e7976ep") collection_ref.add(data) print("已上傳") elif sent[2] == 'Sitting' : if data1['Object'] == ['Jenny','Standing'] : sent[2] = 'Standing' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("1d16e844-d471-4911-95eb-554c14e7976ep") collection_ref.add(data) print("已上傳") elif data1['Object'] == ['Jenny','Lying down'] : sent[2] = 'Lying down' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("1d16e844-d471-4911-95eb-554c14e7976ep") collection_ref.add(data) print("已上傳") elif sent[2] == 'Lying down' : if data1['Object'] == ['Jenny','Sitting'] : sent[2] = 'Sitting' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("1d16e844-d471-4911-95eb-554c14e7976ep") collection_ref.add(data) print("已上傳") elif data1['Object'] == ['Jenny','Standing'] : sent[2] = 'Standing' print(data) try: app = firebase_admin.get_app() except ValueError as e: cred = credentials.Certificate('serviceAccount.json') firebase_admin.initialize_app(cred) db = firestore.client() #collection_ref = db.collection("TimeAndObject").document('ID') collection_ref = db.collection("1d16e844-d471-4911-95eb-554c14e7976ep") collection_ref.add(data) print("已上傳") return img netMain = None metaMain = None altNames = None def YOLO(): global metaMain, netMain, altNames configPath = "./cfg/yolov4-test.cfg" weightPath = "./yolov4-test_last.weights" metaPath = "./data/test.data" #configPath = "./cfg/yolov4.cfg" #weightPath = "./yolov4.weights" #metaPath = "./cfg/coco.data" if not os.path.exists(configPath): raise ValueError("Invalid config path `" + os.path.abspath(configPath)+"`") if not os.path.exists(weightPath): raise ValueError("Invalid weight path `" + os.path.abspath(weightPath)+"`") if not os.path.exists(metaPath): raise ValueError("Invalid data file path `" + os.path.abspath(metaPath)+"`") if netMain is None: netMain = darknet.load_net_custom(configPath.encode( "ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1 if metaMain is None: metaMain = darknet.load_meta(metaPath.encode("ascii")) if altNames is None: try: with open(metaPath) as metaFH: metaContents = metaFH.read() import re match = re.search("names *= *(.*)$", metaContents, re.IGNORECASE | re.MULTILINE) if match: result = match.group(1) else: result = None try: if os.path.exists(result): with open(result) as namesFH: namesList = namesFH.read().strip().split("\n") altNames = [x.strip() for x in namesList] except TypeError: pass except Exception: pass cap = cv2.VideoCapture(0) #cap = cv2.VideoCapture("104.mp4")MJPG CW=1080 CH=1920 cap.set(cv2.CAP_PROP_FRAME_WIDTH,CW) cap.set(cv2.CAP_PROP_FRAME_HEIGHT,CH) out = cv2.VideoWriter( "Demo/output.avi", cv2.VideoWriter_fourcc(*'XVID'), 10.0, (darknet.network_width(netMain), darknet.network_height(netMain))) print("Starting the YOLO loop...") # Create an image we reuse for each detect darknet_image = darknet.make_image(darknet.network_width(netMain), darknet.network_height(netMain),3) while True: prev_time = time.time() ret, frame_read = cap.read() cv2.imshow('OBS', frame_read) frame_resized = cv2.resize(frame_read, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) # 寫入影格 #out.write(frame_resized) frame_rgb = cv2.cvtColor(frame_read, cv2.COLOR_BGR2RGB) frame_resized = cv2.resize(frame_rgb, (darknet.network_width(netMain), darknet.network_height(netMain)), interpolation=cv2.INTER_LINEAR) darknet.copy_image_from_bytes(darknet_image,frame_resized.tobytes()) detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.25) image = cvDrawBoxes(detections, frame_resized) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) #for detection in detections: #label = detection[0] #confidence = detection[1] #pstring = label+": "+str(np.rint(100 * confidence))+"%" #imcaption.append(pstring) cv2.imshow('Demo', image) out.write(image) cv2.waitKey(3) cap.release() out.release() if __name__ == "__main__": YOLO() ``` 辨識結果 === [點此連結觀看辨識結果](https://drive.google.com/file/d/1hy96LQHp2OiJR-pFGeJE5SDXSCeLmv5q/view?usp=sharing) ###### tags: `By 田于昕` --- # Free Video to JPG Converter 主要用途:訓練前labelimg資料庫 方便訓練 ### 到微軟官網下載 https://www.dvdvideosoft.com/products/dvd/Free-Video-to-JPG-Converter.htm ### 執行exe 即可安裝完成 ![](https://i.imgur.com/3Dw9nln.png) ### 執行完會到這個頁面 ![](https://i.imgur.com/rNtwm91.png) 可以選擇擷取方式->可以選擇用秒數或用影片生成照片數 ### 增加檔案成功會跳出 ![](https://i.imgur.com/x154iQr.png) #### 轉換失敗->程序完成 但不會放入圖庫 ![](https://i.imgur.com/r0D3wSp.png) #### 轉換成功->會進入 ![](https://i.imgur.com/1mmxgcy.png) #### 成果 ![](https://i.imgur.com/M7BdtLz.jpg) 遇到的困難 === *安裝python版本要注意 (如果不成功可以看看其他版本) *cuda版本也有差哦 如果不成功的話 可以多把檔案執行幾次 就會成功了! *建置opencv前務必確認前面步驟是否有安裝成功 沒有的話可能會造成建置失敗 *darknet該複製該貼上 請務必準確貼上 貼心小提醒 === 如果有任何建置錯誤google會是你的好朋友 把錯誤貼上任何方法都嘗試看看 多試試看幾種方法 安裝環境非常看電腦 很需要天時地利人和 缺一不可 如果安裝失敗很多次別喪志 很正常的 我安裝YOLO V3一個月才成功 YOLO V4也花半個月哦 辨識要精確框出label不然會影響準確度 遠與近 大與小 這些都需要有素材 拍攝角度也很重要 破除迷失:不是圖片多就會準確 (精確度跟我上述說的有關) 由於我們最終因為時間有限 程式碼偏笨拙 依照擁有聰明腦袋的你們可以好好優化它 有任何問題可以藉由胡老連絡上我們 但!!還是請先自己努力找資料 你們一定比我們聰明不會遇到太多困難的 還有YOLO更新速度非常快 能用最新就用最新 因為辨識速度真的有差 V3跟V4就差很多! 參考安裝網址 === https://medium.com/@qqmanlin/yolo-v4-%E5%AE%89%E8%A3%9D%E6%AD%A5%E9%A9%9F-4cca54d460b1 https://wings890109.pixnet.net/blog/post/68926387-yolov4%E5%BB%BA%E7%BD%AE%E6%B5%81%E7%A8%8B https://www.codenong.com/cs106150135/