yolo的乙個汽車識別專案記錄

2021-09-19 13:26:16 字數 4264 閱讀 5716

def get_image_path(dir):

paths =

for file in os.listdir(dir):

file_path = os.path.join(dir, file)

return paths

定義個:

1、匯入argparse模組

2、建立解析器物件argumentparser,可以新增引數。

description:描述程式

parser=argparse.argumentparser(description="this is a example program ")

add_help:預設是true,可以設定false禁用

3、add_argument()方法,用來指定程式需要接受的命令引數

add_argument()常用的引數:

dest:如果提供dest,例如dest=「a」,那麼可以通過args.a訪問該引數

default:設定引數的預設值

action:引數出發的動作

store:儲存引數,預設

store_const:儲存乙個被定義為引數規格一部分的值(常量),而不是乙個來自引數解析而來的值。

store_ture/store_false:儲存相應的布林值

count:引數出現的次數

parser.add_argument("-v", 「–verbosity」, action=「count」, default=0, help=「increase output verbosity」)

version:列印程式版本資訊

type:把從命令列輸入的結果轉成設定的型別

choice:允許的引數值

parser.add_argument("-v", 「–verbosity」, type=int, choices=[0, 1, 2], help=「increase output verbosity」)

help:引數命令的介紹

parser = argparse.argumentparser()

parser.add_argument('--dir_path', default='test_images', type=str)

parser.add_argument('--out_path', default='out_images', type=str)

parser.add_argument('--model_file', default='model/yolov2-tiny-voc.h5', type=str)

args = parser.parse_args()

得到路徑

paths = utils.get_image_path(args.dir_path)
讀取 進行resize 存到李彪

images = 

for path in paths:

image = cv2.imread(path)

resized = cv2.resize(image, (416, 416))

之後可進行影象處理:濾波之類的

image_processed =

歸一化

def preprocess_image(resized):

out_image = resized/255.

return out_image

批量處理

for image in images:
然後載入模型:

model = load_model(args.model_file)

predictions = model.predict(np.array(image_processed))

畫出框框:

for i in range(predictions.shape[0]):

boxes = utils.process_predictions(predictions[i],probs_threshold=0.3,iou_threshold=0.2)

out_image = utils.draw_boxes(images[i],boxes)

cv2.imwrite('%s/out%s.jpg'%(args.out_path,i), out_image)

畫框子函式有兩個 乙個生成boxs乙個畫出boxs:

def draw_boxes(image,boxes):

for i in range(len(boxes)):

color = colors[boxes[i].clas]

best_class_name = classes[boxes[i].clas]

image = cv2.rectangle(image, (boxes[i].x1, boxes[i].y1),

(boxes[i].x2, boxes[i].y2),color)

# 座標為 左上 右下

cv2.puttext(

image, best_class_name + ' : %.2f' % boxes[i].p_max,

(int(boxes[i].x1 + 5), int(boxes[i].y1 - 7)), cv2.font_hershey_******x, 0.5,

color, 1)

return image

def process_predictions(prediction, n_grid=13, n_class=20, n_box=5, probs_threshold=0.3, iou_threshold=0.3):

prediction = np.reshape(prediction, (n_grid, n_grid, n_box, 5+n_class))

boxes =

for row in range(n_grid):

for col in range(n_grid):

for b in range(n_box):

tx, ty, tw, th, tc = prediction[row, col, b, :5]

box = box()

box.w = np.exp(tw) * anchors[2 * b + 1] * 32.0

box.h = np.exp(th) * anchors[2 * b + 1] * 32.0

c_probs = softmax(prediction[row, col, b, 5:])

box.clas = np.argmax(c_probs)

box.p_max = np.max(c_probs) * sigmoid(tc)

center_x = (float(col) + sigmoid(tx)) * 32.0

center_y = (float(row) + sigmoid(ty)) * 32.0

# center_x = float(col+1)*32.0+tx

# center_y = float(row+1)*32.0+ty

# box.x1 = int(center_x - (tw*10 / 2.)) #得到中心點座標

# box.x2 = int(center_x + (tw*10 / 2.))

# box.y1 = int(center_y - (th*10 / 2.))

# box.y2 = int(center_y + (th*10 / 2.))

# box.x1 = int(center_x - (box.w / 2.)) #得到中心點座標

box.x2 = int(center_x + (box.w / 2.))

box.y1 = int(center_y - (box.h / 2.))

box.y2 = int(center_y + (box.h / 2.))

if box.p_max > probs_threshold:

boxes.sort(key=lambda b: b.p_max, reverse=true)

filtered_boxes = non_maximal_suppression(boxes, iou_threshold)

return filtered_boxes

EasyPR 乙個開源的車牌檢測 識別專案

一.簡介 easypr是乙個中文的開源車牌檢測 識別系統,其目標是成為乙個簡單 高效 準確的車牌識別引擎.相比於其他的車牌識別系統easypr有如下特點 二.專案位址 三.安裝編譯環境 pzs pzs server easypr cat etc os release 系統環境 name ubuntu...

opencv的學習與人臉情緒識別專案(一)

呼叫攝像頭程式 import numpy as np import cv2 呼叫筆記本內建攝像頭,所以引數為0,如果有其他的攝像頭可以調整引數為1,2 cap cv2.videocapture 0 while true 從攝像頭讀取 sucess,img cap.read 轉為灰度 gray cv2...

第乙個專案所學記錄

activity.this的context 返回當前activity的上下文,屬於activity activity 摧毀他就摧毀 jsonparser jsonparser new jsonparser jsonobject json jsonparser.makehttprequest url ...