python import cv2 import numpy as np import tensorflow as tf # 加載模型 model = tf.keras.models.load_model("yolov3.h5") # 定義類別名稱 class_names = ["person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch", "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush"] # 讀取圖像 image = cv2.imread("test.jpg") # 調整圖像大小 image = cv2.resize(image, (416, 416)) # 將圖像轉換為張量 image = tf.convert_to_tensor(image, dtype=tf.float32) image = tf.expand_dims(image, 0) # 進行目標檢測 outputs = model(image) # 處理輸出結果 boxes, scores, classes, nums = outputs # 繪制檢測結果 for i in range(nums[0]): box = boxes[0][i] score = scores[0][i] cls = classes[0][i] label = class_names[int(cls)] if score > 0.5: x1, y1, x2, y2 = box.numpy() x1 = int(x1 * image.shape[2]) y1 = int(y1 * image.shape[1]) x2 = int(x2 * image.shape[2]) y2 = int(y2 * image.shape[1]) cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2) cv2.putText(image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) # 顯示檢測結果 cv2.imshow("image", image.numpy()[0]) cv2.waitKey(0) cv2.destroyAllWindows()在上面的代碼中,我們首先加載了預訓練模型,然后定義了類別名稱。接下來,我們讀取了一張測試圖像,并將其調整為模型的輸入大小。然后,我們將圖像轉換為張量,并將其輸入到模型中進行目標檢測。最后,我們處理模型的輸出結果,并將檢測結果繪制在原始圖像上。 這就是使用 Python 和 TensorFlow 實現 YOLOv3 目標檢測算法的基本步驟。當然,我們還可以對模型進行微調,以提高檢測精度和速度。總之,YOLOv3 是一種非常強大的目標檢測算法,它可以在許多實際應用中發揮重要作用。
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/130969.html
閱讀 3289·2023-04-26 02:40
閱讀 4637·2021-09-22 15:22
閱讀 1572·2021-09-22 10:02
閱讀 3474·2021-08-11 10:23
閱讀 1387·2019-08-30 15:55
閱讀 2486·2019-08-30 12:48
閱讀 583·2019-08-30 11:04
閱讀 696·2019-08-29 16:29