tensorflow图形检测_社交距离检测器——Tensorflow检测模型设计
在隔離期間,我花時間在github上探索Tensorflow的大量預訓練模型。這樣做時,我偶然發現了一個包含25 個帶有性能和速度指標的預訓練對象檢測模型的存儲庫。擁有一些計算機視覺知識并給出了實際的背景知識,我認為使用其中之一來構建社交隔離應用程序可能會很有趣。
更重要的是,上學期在計算機視覺課程中向我介紹了OpenCV,并意識到在執行許多小型項目時它的功能多么強大。其中之一包括執行圖片的鳥瞰轉換。一個鳥瞰圖是一個基本場景的自上而下的表示。這是在構建自動駕駛汽車應用程序時經常執行的任務。
車載攝像頭鳥瞰系統的實現
這使我意識到,將這種技術應用于我們想要監視社會距離的場景可以提高其質量。本文介紹了我如何使用深度學習模型以及計算機視覺方面的一些知識來構建強大的社交距離探測器。
本文的結構如下:
- 選型
- 人物檢測
- 鳥瞰圖轉換
- 社會距離測量
- 結果與改進
以下所有代碼以及安裝說明都可以在我的github存儲庫中找到。
1.選型
Tensorflow對象檢測模型Zoo中可用的所有模型均已在COCO數據集(COntext中的通用對象)上進行了訓練。該數據集包含120,000張圖像,這些圖像中總共有880,000個帶標簽的對象。這些模型經過訓練可以檢測此數據集中標記的90種不同類型的對象。所有這些不同對象的完整列表可在github repo的data部分訪問的存儲庫的data部分中找到。這些對象包括汽車,牙刷,香蕉和人。
可用型號的非詳盡清單
根據模型的速度,它們具有不同的性能。為了確定如何根據預測速度來利用模型的質量,我進行了一些測試。由于此應用程序的目標不是能夠執行實時分析,因此我最終選擇了fast_rcnn_inception_v2_coco ,它的mAP(驗證集上的檢測器性能)為28,非常強大,執行速度為58 ms 。
2.人員檢測
要使用這種模型,為了檢測人員,必須完成一些步驟:
- 將包含模型的文件加載到張量流圖中。并定義您要從模型獲得的輸出。
- 對于每一幀,將圖像通過圖形以獲取所需的輸出。
- 過濾掉不需要檢測的弱預測和對象。
加載并啟動模型
設計張量流模型的工作方式是使用圖形。第一步意味著將模型加載到張量流圖中。該圖將包含為了獲得所需檢測而將要執行的不同操作。下一步是創建一個會話,該會話是負責執行上圖中定義的操作的實體。有關圖形和會話的更多說明,請參見此處。我決定實現一個類,以將與張量流圖有關的所有數據保持在一起。
class Model: """ Class that contains the model and all its functions """ def __init__(self, model_path): """ Initialization function @ model_path : path to the model """ # Declare detection graph self.detection_graph = tf.Graph() # Load the model into the tensorflow graph with self.detection_graph.as_default(): od_graph_def = tf.compat.v1.GraphDef() with tf.io.gfile.GFile(model_path, 'rb') as file: serialized_graph = file.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def, name='') # Create a session from the detection graph self.sess = tf.compat.v1.Session(graph=self.detection_graph) def predict(self,img): """ Get the predicition results on 1 frame @ img : our img vector """ # Expand dimensions since the model expects images to have shape: [1, None, None, 3] img_exp = np.expand_dims(img, axis=0) # Pass the inputs and outputs to the session to get the results (boxes, scores, classes) = self.sess.run([self.detection_graph.get_tensor_by_name('detection_boxes:0'), self.detection_graph.get_tensor_by_name('detection_scores:0'), self.detection_graph.get_tensor_by_name('detection_classes:0')],feed_dict={self.detection_graph.get_tensor_by_name('image_tensor:0'): img_exp}) return (boxes, scores, classes)通過模型傳遞每一幀
對于需要處理的每個幀,都會啟動一個新會話。這是通過調用run()函數來完成的。這樣做時必須指定一些參數。其中包括模型所需的輸入類型以及我們要從中獲取的輸出。在我們的情況下,所需的輸出如下:
- 每個對象的邊界框坐標
- 每個預測的置信度(0到1)
- 預測等級(0到90)
過濾掉弱預測和不相關的對象
人物檢測結果
模型檢測到的許多類之一是人。與一個人關聯的類別為1。
為了排除弱預測(閾值:0.75)和除人以外的所有其他類別的對象,我使用了if語句,將這兩個條件組合在一起以排除任何其他對象以進行進一步計算。
if int(classes[i]) == 1 and scores[i] > 0.75但是,由于這些模型已經過于訓練,因此不可能僅檢測此類。因此,這些模型要花很長時間才能運行,因為它們試圖識別場景中所有90種不同類型的對象。
3.鳥瞰圖轉換
如引言中所述,執行鳥瞰圖轉換可為我們提供場景的俯視圖。值得慶幸的是,OpenCV具有強大的內置功能,可以將該方法應用于圖像,以便將所拍攝的圖像從透視圖角度轉換為頂視圖。我使用了偉大的Adrian Rosebrock 的教程來了解如何做到這一點。
第一步涉及在原始圖像上選擇4個點,這些點將成為要轉換的計劃的拐角點。這一點必須形成一個矩形,其中至少兩個相對的側面平行。如果不這樣做,則轉換發生時的比例將不同。我已經在我的存儲庫中實現了一個腳本,該腳本使用OpenCV 的setMouseCallback()函數來獲取這些坐標。計算變換矩陣的函數還需要使用圖像的image.shape屬性來計算圖像的尺寸。
width, height, _ = image.shape這將返回寬度,高度和其他不相關的彩色像素值。讓我們看看它們如何用于計算轉換矩陣:
def compute_perspective_transform(corner_points,width,height,image):""" Compute the transformation matrix@ corner_points : 4 corner points selected from the image@ height, width : size of the imagereturn : transformation matrix and the transformed image"""# Create an array out of the 4 corner pointscorner_points_array = np.float32(corner_points)# Create an array with the parameters (the dimensions) required to build the matriximg_params = np.float32([[0,0],[width,0],[0,height],[width,height]])# Compute and return the transformation matrixmatrix = cv2.getPerspectiveTransform(corner_points_array,img_params) img_transformed = cv2.warpPerspective(image,matrix,(width,height))return matrix,img_transformed請注意,我選擇還返回矩陣,因為下一步將使用該矩陣來計算每個檢測到的人的新坐標。其結果是幀中每個人的“ GPS”坐標。這是更為準確使用這些不是使用原來的地面點,因為在透視圖中,距離是不一樣的,當人們都在不同的計劃,而不是在從相機相同的距離。與使用原始框架中的點相比,這可以大大改善社會距離度量。
對于檢測到的每個人,將返回構建邊界框所需的2個點。這些點是框的左上角和右下角。從這些中,我通過獲取它們之間的中間點來計算盒子的質心。使用此結果,我計算了位于框底部中心的點的坐標。我認為,這一點(我稱為基點)是圖像中人的坐標的最佳表示。
然后,我使用變換矩陣為每個檢測到的地面點計算變換后的坐標。在檢測到人之后,使用cv2.perspectiveTransform()在每一幀上完成此操作。這就是我實現此任務的方式:
def compute_point_perspective_transformation(matrix,list_downoids):""" Apply the perspective transformation to every ground point which have been detected on the main frame.@ matrix : the 3x3 matrix @ list_downoids : list that contains the points to transformreturn : list containing all the new points"""# Compute the new coordinates of our pointslist_points_to_detect = np.float32(list_downoids).reshape(-1, 1, 2)transformed_points = cv2.perspectiveTransform(list_points_to_detect, matrix)# Loop over the points and add them to the list that will be returnedtransformed_points_list = list()for i in range(0,transformed_points.shape[0]):transformed_points_list.append([transformed_points[i][0][0],transformed_points[i][0][1]])return transformed_points_list4.衡量社會距離
在每幀上調用此函數后,將返回一個包含所有新轉換點的列表。從這個列表中,我不得不計算每對點之間的距離。我使用了來自itertools庫的function Combines ()函數,該函數允許在列表中獲取所有可能的組合而無需保留雙精度。在此堆棧溢出問題上對此進行了很好的解釋。剩余部分是簡單的數學運算:使用python中的math.sqrt()函數很容易實現兩點之間的距離。選擇的閾值為120像素,因為它在我們的場景中大約等于2英尺。
# Check if 2 or more people have been detected (otherwise no need to detect) if len(transformed_downoids) >= 2: # Iterate over every possible 2 by 2 between the points combinations list_indexes = list(itertools.combinations(range(len(transformed_downoids)), 2)) for i,pair in enumerate(itertools.combinations(transformed_downoids, r=2)): # Check if the distance between each combination of points is less than the minimum distance chosen if math.sqrt( (pair[0][0] - pair[1][0])**2 + (pair[0][1] - pair[1][1])**2 ) < int(distance_minimum): # Change the colors of the points that are too close from each other to red change_color_topview(pair) # Get the equivalent indexes of these points in the original frame and change the color to red index_pt1 = list_indexes[i][0] index_pt2 = list_indexes[i][1] change_color_originalframe(index_pt1,index_pt2)一旦確定兩個點之間的距離太近,標記該點的圓圈的顏色將從綠色更改為紅色,并且與原始幀上的邊界框的顏色相同。
5.結果
讓我回復一下該項目的工作方式:
- 首先獲取計劃的4個角點,然后應用透視變換獲得該計劃的鳥瞰圖并保存變換矩陣。
- 獲取原始幀中檢測到的每個人的邊界框。
- 計算此框的最低點。這是位于雙腳之間的點。
- 使用轉換矩陣對這些點中的每一個獲取每個人的真實“ GPS”坐標。
- 使用itertools.combinations()測量框架中每個點到所有其他點的距離。
- 如果檢測到社交距離沖突,請將邊界框的顏色更改為紅色。
總結
以上是生活随笔為你收集整理的tensorflow图形检测_社交距离检测器——Tensorflow检测模型设计的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 悲观的反义词 悲观的反义词有什么
- 下一篇: fyi是什么意思啊 fyi解释