日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

RCAR会议:我的RTFA算法里面的generate_detections.py文件

發(fā)布時間:2024/1/1 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 RCAR会议:我的RTFA算法里面的generate_detections.py文件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

地址: nk_DeepSortYolo/deep_sort_yolov3-master/tools/generate_detections.py

# vim: expandtab:ts=4:sw=4 import os import errno import argparse import numpy as np import cv2 import tensorflow as tf from PIL import Image # from PIL import Image 配合  # TODO 放縮def _run_in_batches(f, data_dict, out, batch_size): # TODO 4data_len = len(out)# print(data_len)為人的個數(shù)num_batches = int(data_len / batch_size)s, e = 0, 0for i in range(num_batches):s, e = i * batch_size, (i + 1) * batch_sizebatch_data_dict = {k: v[s:e] for k, v in data_dict.items()}out[s:e] = f(batch_data_dict)if e < len(out):batch_data_dict = {k: v[e:] for k, v in data_dict.items()}out[e:] = f(batch_data_dict)# 我們引用了新的放縮方法(此處) def scaling_extract_image_patch: 來讓動物的全身特征都加入特征內(nèi)放縮為(64,128) # 而不是(下面)的 def extract_image_patch: 提取圖片切片是不包含放縮小的直接切成(64,128)寬高的人性切片, # 但是放縮方法并不好用在行人reid # TODO 5 一:放縮法 x方向 def scaling_x_extract_image_patch(image, bbox, patch_shape): # patch_shape = image_shape: [128, 64, 3]# patch_shape = image_shape[:2]=[128, 64]"""Extract image patch from bounding box.Parameters----------image : ndarrayThe full image.bbox : array_likeThe bounding box in format (x, y, width, height).這里的xy指的是左上角點的坐標(biāo)patch_shape : Optional[array_like]This parameter can be used to enforce a desired patch shape(height, width). First, the `bbox` is adapted to the aspect ratioof the patch shape, then it is clipped at the image boundaries.If None, the shape is computed from :arg:`bbox`.Returns-------ndarray | NoneTypeAn image patch showing the :arg:`bbox`, optionally reshaped to:arg:`patch_shape`.Returns None if the bounding box is empty or fully outside of the imageboundaries.如果bbox空的或者完全離開圖像邊界,就返回none"""# print('bbox:', bbox) # 列表bbox = np.array(bbox) # 現(xiàn)在是數(shù)組# print('bbox:', bbox)# bbox1 = bbox# bbox1[2:] += bbox1[:2]# bbox1 = bbox1.astype(np.int)# sx1, sy1, ex1, ey1 = bbox1# print('bbox:', bbox) # 數(shù)組# 只改變寬度來適應(yīng)縱橫比'''省略了這里面的剪裁步驟,轉(zhuǎn)而進(jìn)行下面的放縮過程 # TODO 放縮if patch_shape is not None: # patch_shape為image_shape[:2]=[128, 64] 高寬# correct aspect ratio to patch shapetarget_aspect = float(patch_shape[1]) / patch_shape[0] # 寬/高 height bbox[3]高在這里沒有改變,只改變寬度new_width = target_aspect * bbox[3] # bbox[3] height# 新的寬度 = 寬/高  ×bbox[高]bbox[0] -= (new_width - bbox[2]) / 2 # 中心點x,這里的新的寬度一般是小于原來寬度,所以--為+最后是加# bbox(x, y, width, height)# bbox[2]是原來的寬,  x - (新寬 - 原來寬)/2bbox[2] = new_width# bbox 新的xywh'''# convert (x,y,width,height) to ( left,top,right, bottom ) ltrbbbox[2:] += bbox[:2]bbox = bbox.astype(np.int)# clip at image boundariesbbox[:2] = np.maximum(0, bbox[:2]) # top,left 取非負(fù)bbox[2:] = np.minimum(np.asarray(image.shape[:2][::-1]) - 1, bbox[2:]) # right,bottom不要過原圖像右下界限if np.any(bbox[:2] >= bbox[2:]): # 圖像top left 不能大于 right bottomreturn Nonesx, sy, ex, ey = bbox # x左 y上 x右 y下# print('class:', type(image)) # <class 'numpy.ndarray'># print('shape:', image.shape) # (480, 640, 3)image = image[sy:ey, sx:ex] # y方向,x方向 <class 'numpy.ndarray'># image1 = image[sy1:ey1, sx1:ex1]# TODO 放縮\# print('class:', type(image)) # <class 'numpy.ndarray'># image = np.rot90(image)# TODO 放縮0 numpy 截取出來的最初結(jié)果cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-scale-x1' + ".jpg", image)# print('patch_shape', patch_shape)image = cv2.resize(image, (patch_shape[1], patch_shape[0])) # TODO 此步驟可以取代后面 放縮1,2,3,4步驟# patch_shape = [128, 64]cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-scale-x2-zoom' + ".jpg", image)'''# 注釋:因為我沒有發(fā)現(xiàn)cv2針對<class 'numpy.ndarray'>格式圖片有放縮函數(shù) cv2.resize(image, (64, 128)),# 因而采取了下面轉(zhuǎn)換為Image格式在用 Image里面的image.resize((64, 128))函數(shù),再轉(zhuǎn)換為<class 'numpy.ndarray'>格式的笨方法.# 并且此方法還會導(dǎo)致色彩錯亂,下面地址可見案例/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/色彩錯亂案例# TODO 放縮1 image 格式轉(zhuǎn)換為Imageimage = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) # 從cv格式轉(zhuǎn)換為Image格式 image.save('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-1' + ".jpg")# print('class', type(image)) # <class 'PIL.Image.Image'># image = image.rotate(-90) # -90是順時針旋轉(zhuǎn)90度# TODO 放縮2 image 放縮為 寬高1:2image = image.resize((64, 128)) # 調(diào)整圖片大小# 此時image <class 'PIL.Image.Image'>image.save('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-2' + ".jpg")# TODO 放縮3 numpy 再從image格式轉(zhuǎn)換為numpy格式image = np.reshape(image, (128, 64, 3)) # 如果是灰度圖片 把3改為-1# print('1', type(image)) <class 'numpy.ndarray'>cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-3' + ".jpg", image)# cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-2' + ".jpg", image1)# TODO 放縮4 (此步驟貌似可以省略)image = cv2.resize(image, tuple(patch_shape[::-1])) # resize需要(寬,高)格式 patch_shape 是[高,寬]# print('2', type(image)) # <class 'numpy.ndarray'>cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-4' + ".jpg", image)# resize需要(寬,高)格式 image_shape高寬為[128, 64, 3] # image_shape[:2]= patch_shape 是[高,寬]'''# TODO 放縮/return image# 我們引用了新的放縮方法(此處) def scaling_extract_image_patch: 來讓動物的全身特征都加入特征內(nèi)放縮為(64,128) # 而不是(下面)的 def extract_image_patch: 提取圖片切片是不包含放縮小的直接切成(64,128)寬高的人性切片, # 但是放縮方法并不好用在行人reid# TODO 5 二(90):放縮法 y方向 def scaling_y_extract_image_patch(image, bbox, patch_shape):# patch_shape = image_shape[:2]=[128, 64]"""Extract image patch from bounding box.Parameters----------image : ndarrayThe full image.bbox : array_likeThe bounding box in format (x, y, width, height).這里的xy指的是左上角點的坐標(biāo)patch_shape : Optional[array_like]This parameter can be used to enforce a desired patch shape(height, width). First, the `bbox` is adapted to the aspect ratioof the patch shape, then it is clipped at the image boundaries.If None, the shape is computed from :arg:`bbox`.Returns-------ndarray | NoneTypeAn image patch showing the :arg:`bbox`, optionally reshaped to:arg:`patch_shape`.Returns None if the bounding box is empty or fully outside of the imageboundaries.如果bbox空的或者完全離開圖像邊界,就返回none"""# print('bbox:', bbox) # 列表bbox = np.array(bbox) # 現(xiàn)在是數(shù)組# print('bbox:', bbox)# bbox1 = bbox# bbox1[2:] += bbox1[:2]# bbox1 = bbox1.astype(np.int)# sx1, sy1, ex1, ey1 = bbox1# print('bbox:', bbox) # 數(shù)組# 只改變寬度來適應(yīng)縱橫比'''省略了這里面的剪裁步驟,轉(zhuǎn)而進(jìn)行下面的放縮過程 # TODO 放縮if patch_shape is not None: # patch_shape為image_shape[:2]=[128, 64] 高寬# correct aspect ratio to patch shapetarget_aspect = float(patch_shape[1]) / patch_shape[0] # 寬/高 height bbox[3]高在這里沒有改變,只改變寬度new_width = target_aspect * bbox[3] # bbox[3] height# 新的寬度 = 寬/高  ×bbox[高]bbox[0] -= (new_width - bbox[2]) / 2 # 中心點x,這里的新的寬度一般是小于原來寬度,所以--為+最后是加# bbox(x, y, width, height)# bbox[2]是原來的寬,  x - (新寬 - 原來寬)/2bbox[2] = new_width# bbox 新的xywh'''# convert (x,y,width,height) to ( left,top,right, bottom ) ltrbbbox[2:] += bbox[:2]bbox = bbox.astype(np.int)# clip at image boundariesbbox[:2] = np.maximum(0, bbox[:2]) # top,left 取非負(fù)bbox[2:] = np.minimum(np.asarray(image.shape[:2][::-1]) - 1, bbox[2:]) # right,bottom不要過原圖像右下界限if np.any(bbox[:2] >= bbox[2:]): # 圖像top left 不能大于 right bottomreturn Nonesx, sy, ex, ey = bbox # x左 y上 x右 y下# print('class:', type(image)) # <class 'numpy.ndarray'># print('shape:', image.shape) # (480, 640, 3)image = image[sy:ey, sx:ex] # y方向,x方向 <class 'numpy.ndarray'># image1 = image[sy1:ey1, sx1:ex1]# TODO 放縮\# print('class:', type(image)) # <class 'numpy.ndarray'># image = np.rot90(image)# TODO 放縮0 numpy 截取出來的最初結(jié)果cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-scale90-y1' + ".jpg", image)image = cv2.resize(image, (patch_shape[0], patch_shape[1])) # TODO 此步驟可以取代后面 放縮1,2,3,4步驟# patch_shape = [128, 64]cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-scale90-y2-zoom' + ".jpg", image)image = np.rot90(image)# print('y90', image.shape)cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-scale90-y3-rot90' + ".jpg", image)'''旋轉(zhuǎn) 不好用, 報錯參數(shù)是浮點形應(yīng)該是整形# rows, cols, channel = image.shape# rows = int(rows)# cols = int(cols)# print('rows, cols, channel:', rows, ' ', cols, ' ', channel)# change = cv2.getRotationMatrix2D((rows//2, cols//2), 90, 1)# image = cv2.warpAffine(image, change, (rows, cols))'''# cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-2' + ".jpg", image)'''# 注釋:因為我沒有發(fā)現(xiàn)cv2針對<class 'numpy.ndarray'>格式圖片有放縮函數(shù) cv2.resize(image, (64, 128)),# 因而采取了下面轉(zhuǎn)換為Image格式在用 Image里面的image.resize((64, 128))函數(shù),再轉(zhuǎn)換為<class 'numpy.ndarray'>格式的笨方法.# 并且此方法還會導(dǎo)致色彩錯亂,下面地址可見案例/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/色彩錯亂案例# TODO 放縮1 image 格式轉(zhuǎn)換為Imageimage = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) # 從cv格式轉(zhuǎn)換為Image格式 image.save('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-1' + ".jpg")# print('class', type(image)) # <class 'PIL.Image.Image'># image = image.rotate(-90) # -90是順時針旋轉(zhuǎn)90度# TODO 放縮2 image 放縮為 寬高1:2image = image.resize((64, 128)) # 調(diào)整圖片大小# 此時image <class 'PIL.Image.Image'>image.save('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-2' + ".jpg")# TODO 放縮3 numpy 再從image格式轉(zhuǎn)換為numpy格式image = np.reshape(image, (128, 64, 3)) # 如果是灰度圖片 把3改為-1# print('1', type(image)) <class 'numpy.ndarray'>cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-3' + ".jpg", image)# cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-2' + ".jpg", image1)# TODO 放縮4 (此步驟貌似可以省略)image = cv2.resize(image, tuple(patch_shape[::-1])) # resize需要(寬,高)格式 patch_shape 是[高,寬]# print('2', type(image)) # <class 'numpy.ndarray'>cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-4' + ".jpg", image)# resize需要(寬,高)格式 image_shape高寬為[128, 64, 3] # image_shape[:2]= patch_shape 是[高,寬]'''# TODO 放縮/return image# TODO 5 二(270):放縮法 y方向 def scaling_y_270_extract_image_patch(image, bbox, patch_shape):# patch_shape = image_shape[:2]=[128, 64]"""Extract image patch from bounding box.Parameters----------image : ndarrayThe full image.bbox : array_likeThe bounding box in format (x, y, width, height).這里的xy指的是左上角點的坐標(biāo)patch_shape : Optional[array_like]This parameter can be used to enforce a desired patch shape(height, width). First, the `bbox` is adapted to the aspect ratioof the patch shape, then it is clipped at the image boundaries.If None, the shape is computed from :arg:`bbox`.Returns-------ndarray | NoneTypeAn image patch showing the :arg:`bbox`, optionally reshaped to:arg:`patch_shape`.Returns None if the bounding box is empty or fully outside of the imageboundaries.如果bbox空的或者完全離開圖像邊界,就返回none"""# print('bbox:', bbox) # 列表bbox = np.array(bbox) # 現(xiàn)在是數(shù)組# print('bbox:', bbox)# bbox1 = bbox# bbox1[2:] += bbox1[:2]# bbox1 = bbox1.astype(np.int)# sx1, sy1, ex1, ey1 = bbox1# print('bbox:', bbox) # 數(shù)組# 只改變寬度來適應(yīng)縱橫比'''省略了這里面的剪裁步驟,轉(zhuǎn)而進(jìn)行下面的放縮過程 # TODO 放縮if patch_shape is not None: # patch_shape為image_shape[:2]=[128, 64] 高寬# correct aspect ratio to patch shapetarget_aspect = float(patch_shape[1]) / patch_shape[0] # 寬/高 height bbox[3]高在這里沒有改變,只改變寬度new_width = target_aspect * bbox[3] # bbox[3] height# 新的寬度 = 寬/高  ×bbox[高]bbox[0] -= (new_width - bbox[2]) / 2 # 中心點x,這里的新的寬度一般是小于原來寬度,所以--為+最后是加# bbox(x, y, width, height)# bbox[2]是原來的寬,  x - (新寬 - 原來寬)/2bbox[2] = new_width# bbox 新的xywh'''# convert (x,y,width,height) to ( left,top,right, bottom ) ltrbbbox[2:] += bbox[:2]bbox = bbox.astype(np.int)# clip at image boundariesbbox[:2] = np.maximum(0, bbox[:2]) # top,left 取非負(fù)bbox[2:] = np.minimum(np.asarray(image.shape[:2][::-1]) - 1, bbox[2:]) # right,bottom不要過原圖像右下界限if np.any(bbox[:2] >= bbox[2:]): # 圖像top left 不能大于 right bottomreturn Nonesx, sy, ex, ey = bbox # x左 y上 x右 y下# print('class:', type(image)) # <class 'numpy.ndarray'># print('shape:', image.shape) # (480, 640, 3)image = image[sy:ey, sx:ex] # y方向,x方向 <class 'numpy.ndarray'># image1 = image[sy1:ey1, sx1:ex1]# TODO 放縮\# print('class:', type(image)) # <class 'numpy.ndarray'># image = np.rot90(image)# TODO 放縮0 numpy 截取出來的最初結(jié)果cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-scale270-y1' + ".jpg", image)image = cv2.resize(image, (patch_shape[0], patch_shape[1])) # TODO 此步驟可以取代后面 放縮1,2,3,4步驟cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-scale270-y2-zoom' + ".jpg", image)image = cv2.flip(image, 0, dst=None) # 水平鏡像cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-scale270-y2-flip' + ".jpg", image)image = np.rot90(image)# print('y270', image.shape)cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-scale270-y2-rot90' + ".jpg", image)'''旋轉(zhuǎn) 不好用, 報錯參數(shù)是浮點形應(yīng)該是整形# rows, cols, channel = image.shape# rows = int(rows)# cols = int(cols)# print('rows, cols, channel:', rows, ' ', cols, ' ', channel)# change = cv2.getRotationMatrix2D((rows//2, cols//2), 90, 1)# image = cv2.warpAffine(image, change, (rows, cols))'''# cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-2' + ".jpg", image)'''# 注釋:因為我沒有發(fā)現(xiàn)cv2針對<class 'numpy.ndarray'>格式圖片有放縮函數(shù) cv2.resize(image, (64, 128)),# 因而采取了下面轉(zhuǎn)換為Image格式在用 Image里面的image.resize((64, 128))函數(shù),再轉(zhuǎn)換為<class 'numpy.ndarray'>格式的笨方法.# 并且此方法還會導(dǎo)致色彩錯亂,下面地址可見案例/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/色彩錯亂案例# TODO 放縮1 image 格式轉(zhuǎn)換為Imageimage = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) # 從cv格式轉(zhuǎn)換為Image格式 image.save('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-1' + ".jpg")# print('class', type(image)) # <class 'PIL.Image.Image'># image = image.rotate(-90) # -90是順時針旋轉(zhuǎn)90度# TODO 放縮2 image 放縮為 寬高1:2image = image.resize((64, 128)) # 調(diào)整圖片大小# 此時image <class 'PIL.Image.Image'>image.save('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-2' + ".jpg")# TODO 放縮3 numpy 再從image格式轉(zhuǎn)換為numpy格式image = np.reshape(image, (128, 64, 3)) # 如果是灰度圖片 把3改為-1# print('1', type(image)) <class 'numpy.ndarray'>cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-3' + ".jpg", image)# cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-2' + ".jpg", image1)# TODO 放縮4 (此步驟貌似可以省略)image = cv2.resize(image, tuple(patch_shape[::-1])) # resize需要(寬,高)格式 patch_shape 是[高,寬]# print('2', type(image)) # <class 'numpy.ndarray'>cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-4' + ".jpg", image)# resize需要(寬,高)格式 image_shape高寬為[128, 64, 3] # image_shape[:2]= patch_shape 是[高,寬]'''# TODO 放縮/return image# 此處的提取圖片切片是不包含放縮小的直接切成(64,128)寬高的人性切片, # 因為我們引用了新的放縮方法(在上面) def scaling_extract_image_patch: 來讓動物的全身特征都加入特征內(nèi)放縮為(64,128) # TODO 5 三:切割法 x方向 def extract_image_patch(image, bbox, patch_shape):# patch_shape = image_shape[:2]=[128, 64]"""Extract image patch from bounding box.Parameters----------image : ndarrayThe full image.bbox : array_likeThe bounding box in format (x, y, width, height).這里的xy指的是左上角點的坐標(biāo)patch_shape : Optional[array_like]This parameter can be used to enforce a desired patch shape(height, width). First, the `bbox` is adapted to the aspect ratioof the patch shape, then it is clipped at the image boundaries.If None, the shape is computed from :arg:`bbox`.Returns-------ndarray | NoneTypeAn image patch showing the :arg:`bbox`, optionally reshaped to:arg:`patch_shape`.Returns None if the bounding box is empty or fully outside of the imageboundaries.如果bbox空的或者完全離開圖像邊界,就返回none"""# print('bbox:', bbox) # 列表bbox = np.array(bbox)# bbox1 = bbox# bbox1[2:] += bbox1[:2]# bbox1 = bbox1.astype(np.int)# sx1, sy1, ex1, ey1 = bbox1# print('bbox:', bbox) # 數(shù)組# 只改變寬度來適應(yīng)縱橫比if patch_shape is not None: # patch_shape為image_shape[:2]=[128, 64] 高寬# correct aspect ratio to patch shapetarget_aspect = float(patch_shape[1]) / patch_shape[0] # 寬/高 height bbox[3]高在這里沒有改變,只改變寬度new_width = target_aspect * bbox[3] # bbox[3] height# 新的寬度 = 寬/高  ×bbox[高]bbox[0] -= (new_width - bbox[2]) / 2 # 中心點x,這里的新的寬度一般是小雨原來寬度,所以--為+最后是加# bbox(x, y, width, height)# bbox[2]是原來的寬,  x - (新寬 - 原來寬)/2bbox[2] = new_width# bbox 新的xywh# convert (x,y,width,height) to ( left,top,right, bottom ) ltrbbbox[2:] += bbox[:2]bbox = bbox.astype(np.int)# clip at image boundariesbbox[:2] = np.maximum(0, bbox[:2]) # top,left 取非負(fù)bbox[2:] = np.minimum(np.asarray(image.shape[:2][::-1]) - 1, bbox[2:]) # right,bottom不要過原圖像右下界限if np.any(bbox[:2] >= bbox[2:]): # 圖像top left 不能大于 right bottomreturn Nonesx, sy, ex, ey = bbox # x左 y上 x右 y下image = image[sy:ey, sx:ex] # y方向,x方向# image1 = image[sy1:ey1, sx1:ex1]cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-cut-x1' + ".jpg", image)# cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-2' + ".jpg", image1)image = cv2.resize(image, tuple(patch_shape[::-1])) # <class 'numpy.ndarray'>cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-cut-x2-zoom' + ".jpg", image)# resize需要(寬,高)格式 image_shape高寬為[128, 64, 3] # image_shape[:2]= patch_shape 是[高,寬]return image# 此處的提取圖片切片是不包含放縮小的直接切成(64,128)寬高的人性切片, # 因為我們引用了新的放縮方法(在上面) def scaling_extract_image_patch: 來讓動物的全身特征都加入特征內(nèi)放縮為(64,128)# TODO 5 四(90):切割法 y方向 def y_extract_image_patch(image, bbox, patch_shape):# patch_shape = image_shape[:2]=[128, 64]"""Extract image patch from bounding box.Parameters----------image : ndarrayThe full image.bbox : array_likeThe bounding box in format (x, y, width, height).這里的xy指的是左上角點的坐標(biāo)patch_shape : Optional[array_like]This parameter can be used to enforce a desired patch shape(height, width). First, the `bbox` is adapted to the aspect ratioof the patch shape, then it is clipped at the image boundaries.If None, the shape is computed from :arg:`bbox`.Returns-------ndarray | NoneTypeAn image patch showing the :arg:`bbox`, optionally reshaped to:arg:`patch_shape`.Returns None if the bounding box is empty or fully outside of the imageboundaries.如果bbox空的或者完全離開圖像邊界,就返回none"""# print('bbox:', bbox) # 列表bbox = np.array(bbox)# bbox1 = bbox# bbox1[2:] += bbox1[:2]# bbox1 = bbox1.astype(np.int)# sx1, sy1, ex1, ey1 = bbox1# print('bbox:', bbox) # 數(shù)組# 只改變寬度來適應(yīng)縱橫比if patch_shape is not None: # patch_shape為image_shape[:2]=[128, 64] 高寬# correct aspect ratio to patch shape# patch_shape[1] = 64'''錯誤示范target_aspect = float(patch_shape[0]) / patch_shape[1] # 128/64 height bbox[3]高在這里沒有改變,只改變寬度new_width = target_aspect * bbox[3] # bbox[3] height# 新的寬度 = 寬/高(1/2)  ×bbox[高]bbox[0] -= (new_width - bbox[2]) / 2 # 中心點x,這里的新的寬度一般是小雨原來寬度,所以--為+最后是加# bbox(x, y, width, height)# bbox[2]是原來的寬,  x - (新寬 - 原來寬)/2bbox[2] = new_width# bbox 新的xywh'''###target_aspect = float(patch_shape[1]) / patch_shape[0] # 64/128 height bbox[3]高在這里沒有改變,只改變寬度new_height = target_aspect * bbox[2] # bbox[2] width# 新的高 = 寬/高(1/2)  ×bbox[寬]bbox[1] -= (new_height - bbox[3]) / 2 # 中心點x,這里的新的寬度一般是小雨原來寬度,所以--為+最后是加# bbox(x, y, width, height)# bbox[2]是原來的寬,  x - (新寬 - 原來寬)/2bbox[3] = new_height# bbox 新的xywh#### convert (x,y,width,height) to ( left,top,right, bottom ) ltrbbbox[2:] += bbox[:2]bbox = bbox.astype(np.int)# clip at image boundariesbbox[:2] = np.maximum(0, bbox[:2]) # top,left 取非負(fù)bbox[2:] = np.minimum(np.asarray(image.shape[:2][::-1]) - 1, bbox[2:]) # right,bottom不要過原圖像右下界限if np.any(bbox[:2] >= bbox[2:]): # 圖像top left 不能大于 right bottomreturn Nonesx, sy, ex, ey = bbox # x左 y上 x右 y下image = image[sy:ey, sx:ex] # y方向,x方向# image1 = image[sy1:ey1, sx1:ex1]cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-cut90-y1' + ".jpg", image)# cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-2' + ".jpg", image1)image = cv2.resize(image, tuple(patch_shape)) # <class 'numpy.ndarray'>cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-cut90-y2-zoom' + ".jpg", image)image = np.rot90(image, 1) # 地面朝向右邊cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-cut90-y3-rot90' + ".jpg", image)# resize需要(寬,高)格式 image_shape高寬為[128, 64, 3] # image_shape[:2]= patch_shape 是[高,寬]return image# TODO 5 四(270):切割法 y方向 def y_270_extract_image_patch(image, bbox, patch_shape):# patch_shape = image_shape[:2]=[128, 64]"""Extract image patch from bounding box.Parameters----------image : ndarrayThe full image.bbox : array_likeThe bounding box in format (x, y, width, height).這里的xy指的是左上角點的坐標(biāo)patch_shape : Optional[array_like]This parameter can be used to enforce a desired patch shape(height, width). First, the `bbox` is adapted to the aspect ratioof the patch shape, then it is clipped at the image boundaries.If None, the shape is computed from :arg:`bbox`.Returns-------ndarray | NoneTypeAn image patch showing the :arg:`bbox`, optionally reshaped to:arg:`patch_shape`.Returns None if the bounding box is empty or fully outside of the imageboundaries.如果bbox空的或者完全離開圖像邊界,就返回none"""# print('bbox:', bbox) # 列表bbox = np.array(bbox)# bbox1 = bbox# bbox1[2:] += bbox1[:2]# bbox1 = bbox1.astype(np.int)# sx1, sy1, ex1, ey1 = bbox1# print('bbox:', bbox) # 數(shù)組# 只改變寬度來適應(yīng)縱橫比if patch_shape is not None: # patch_shape為image_shape[:2]=[128, 64] 高寬# correct aspect ratio to patch shape# patch_shape[1] = 64target_aspect = float(patch_shape[1]) / patch_shape[0] # 64/128 height bbox[3]高在這里沒有改變,只改變寬度new_height = target_aspect * bbox[2] # bbox[2] width# 新的高 = 寬/高(1/2)  ×bbox[寬]bbox[1] -= (new_height - bbox[3]) / 2 # 中心點x,這里的新的寬度一般是小雨原來寬度,所以--為+最后是加# bbox(x, y, width, height)# bbox[2]是原來的寬,  x - (新寬 - 原來寬)/2bbox[3] = new_height# bbox 新的xywh# convert (x,y,width,height) to ( left,top,right, bottom ) ltrbbbox[2:] += bbox[:2]bbox = bbox.astype(np.int)# clip at image boundariesbbox[:2] = np.maximum(0, bbox[:2]) # top,left 取非負(fù)bbox[2:] = np.minimum(np.asarray(image.shape[:2][::-1]) - 1, bbox[2:]) # right,bottom不要過原圖像右下界限if np.any(bbox[:2] >= bbox[2:]): # 圖像top left 不能大于 right bottomreturn Nonesx, sy, ex, ey = bbox # x左 y上 x右 y下image = image[sy:ey, sx:ex] # y方向,x方向# image1 = image[sy1:ey1, sx1:ex1]cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-cut270-y1' + ".jpg", image)# cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-2' + ".jpg", image1)image = cv2.resize(image, tuple(patch_shape)) # <class 'numpy.ndarray'>cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-cut270-y2-zoom' + ".jpg", image)image = cv2.flip(image, 1, dst=None) # 水平鏡像cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-cut270-y2-mirror' + ".jpg", image)image = np.rot90(image, 1) # 地面朝向右邊cv2.imwrite('/home/hp/zjc/nk_PyCharm/PyCharm_project/nk_DeepSortYolo/train/' + '-cut270-y3_rot90' + ".jpg", image)# resize需要(寬,高)格式 image_shape高寬為[128, 64, 3] # image_shape[:2]= patch_shape 是[高,寬]return image''' # TODO 5 一:放縮法 x方向 def scaling_x_extract_image_patch(image, bbox, patch_shape): # patch_shape = image_shape: [128, 64, 3] # TODO 5 二(90):放縮法 y方向 def scaling_y_extract_image_patch(image, bbox, patch_shape): # TODO 5 二(270):放縮法 y方向 def scaling_y_270_extract_image_patch(image, bbox, patch_shape): # TODO 5 三:切割法 x方向 def extract_image_patch(image, bbox, patch_shape): # TODO 5 四:切割法(90) y方向 def y_extract_image_patch(image, bbox, patch_shape): # TODO 5 四:切割法(270) y方向 def y_270_extract_image_patch(image, bbox, patch_shape): '''# TODO 被特征1,2調(diào)用: 被下面 def create_box_encoder_xy() 與 def create_box_encoder() 調(diào)用 class ImageEncoder(object): # TODO ImageEncoderdef __init__(self, checkpoint_filename, input_name="images",output_name="features"):self.session = tf.Session()with tf.gfile.GFile(checkpoint_filename, "rb") as file_handle: # TODO 2 學(xué)習(xí)tf這部分graph_def = tf.GraphDef()# tf.gfile.GFile(filename, mode)# 獲取文本操作句柄,類似于python提供的文本操作open()函數(shù),filename是要打開的文件名,# mode是以何種方式去讀寫,將會返回一個文本操作句柄。graph_def.ParseFromString(file_handle.read())tf.import_graph_def(graph_def, name="net")# 將圖從graph_def導(dǎo)入到當(dāng)前默認(rèn)圖中.# graph_def: 包含要導(dǎo)入到默認(rèn)圖中的操作的GraphDef proto。self.input_var = tf.get_default_graph().get_tensor_by_name("net/%s:0" % input_name)print('self.input_var:', self.input_var)# input_var: Tensor("net/images:0", shape=(?, 128, 64, 3), dtype=uint8)self.output_var = tf.get_default_graph().get_tensor_by_name("net/%s:0" % output_name)print('self.output_name:', self.output_var)# output_name: Tensor("net/features:0", shape=(?, 128), dtype=float32)# print('self.input_var', self.input_var) Tensor("net/images:0", shape=(?, 128, 64, 3), dtype=uint8)# print('self.output_var', self.output_var) Tensor("net/features:0", shape=(?, 128), dtype=float32)assert len(self.output_var.get_shape()) == 2assert len(self.input_var.get_shape()) == 4self.feature_dim = self.output_var.get_shape().as_list()[-1]self.image_shape = self.input_var.get_shape().as_list()[1:]# print('self.feature_dim', self.feature_dim) 128# print('第一個shape:', self.image_shape) 高寬為[128, 64, 3]def __call__(self, data_x, batch_size=32):out = np.zeros((len(data_x), self.feature_dim), np.float32)# print(data_x) # 為(1, 128, 64, 3)的四維數(shù)組多層嵌套,兩個人就1變2# print(data_x.shape)# print(len(data_x)) # 為1# print('zeros:', out.shape) # 為(1,128)一個人,128特征 [[0.17.., ... ,0.066..]]數(shù)組_run_in_batches(lambda x: self.session.run(self.output_var, feed_dict=x),{self.input_var: data_x}, out, batch_size)# def _run_in_batches(f, data_dict, out, batch_size):# f# data_dict {self.input_var: data_x}# out.shape為(1,128)一個人,128特征 數(shù)組形式# batch_size為1# TODO 3 print('ImageEncoder結(jié)果輸出out為:', '\n', out, '\n', out.shape)return out# TODO 特征1: x方向, y90方向, y270方向, 三特征綜合考慮 def create_box_encoder_xy(model_filename, input_name="images",output_name="features", batch_size=32): # TODO 1 encoder(model_filename, batch_size=1)image_encoder = ImageEncoder(model_filename, input_name, output_name) # TODO ImageEncoder# image_encoder= out [[0.17.., ... ,0.066..][..., ..., ...]] (2,128) 數(shù)組含義,兩個人,128特征# print('image_encoder:', image_encoder)# image_encoder: <tools.generate_detections.ImageEncoder object at 0x7efc20031f98>image_shape = image_encoder.image_shape# print('image_shape:', image_shape)# image_shape: [128, 64, 3]# print('image_shape:', image_shape) # self.image_shape高寬為[128, 64, 3]def encoder(image, boxes):print('-->三特征create_box_encoder_xy')image_patches = []image_patches_y = [] # todo yimage_patches_y_270 = [] # todo y270for box in boxes: # 此處循環(huán), 針對每個目標(biāo)進(jìn)行處理patch = scaling_x_extract_image_patch(image, box, image_shape[:2]) # TODO 5patch_y = scaling_y_extract_image_patch(image, box, image_shape[:2]) # todo ypatch_y_270 = scaling_y_270_extract_image_patch(image, box, image_shape[:2]) # todo y270'''# TODO 5 一:放縮法 x方向def scaling_x_extract_image_patch(image, bbox, patch_shape): # patch_shape = image_shape: [128, 64, 3]# TODO 5 二(90):放縮法 y方向def scaling_y_extract_image_patch(image, bbox, patch_shape):# TODO 5 二(270):放縮法 y方向def scaling_y_270_extract_image_patch(image, bbox, patch_shape):# TODO 5 三:切割法 x方向def extract_image_patch(image, bbox, patch_shape):# TODO 5 四:切割法(90) y方向def y_extract_image_patch(image, bbox, patch_shape):# TODO 5 四:切割法(270) y方向'''# patch = extract_image_patch(image, box, image_shape[:2]) # TODO 5# patch_y = y_extract_image_patch(image, box, image_shape[:2]) # todo y# patch_y_270 = y_270_extract_image_patch(image, box, image_shape[:2]) # todo y270print('pathce.shape:', patch.shape) # features.shape (1, 128)print('pathce_y.shape:', patch_y.shape) # features_y.shape (2, 128)print('pathce_y_270.shape:', patch_y_270.shape) # features__270.shape (0, 128)s# TODO 5 extract_image_patch & scaling_x_extract_image_patch# print('格式:', type(patch)) # <class 'numpy.ndarray'># image_shape[:2]=[128, 64]# type(patch)是numpy.ndarray數(shù)組# TODO patch是正常的縱向reid,patch2,patch3是橫向if patch is None:print("WARNING: Failed to extract image patch: %s." % str(box))patch = np.random.uniform(0., 255., image_shape).astype(np.uint8) # 在0到255范圍內(nèi)隨機取,矩陣是[128,64]的隨機數(shù)if patch_y is None: # todo yprint("WARNING: Failed to extract image patch: %s." % str(box))patch_y = np.random.uniform(0., 255., image_shape).astype(np.uint8) # 在0到255范圍內(nèi)隨機取,矩陣是[128,64]的隨機數(shù)if patch_y_270 is None: # todo yprint("WARNING: Failed to extract image patch: %s." % str(box))patch_y_270 = np.random.uniform(0., 255., image_shape).astype(np.uint8) # 在0到255范圍內(nèi)隨機取,矩陣是[128,64]的隨機數(shù)image_patches.append(patch) # 出現(xiàn)了幾個人,就有幾個box,就有幾個patch <class 'list'>image_patches_y.append(patch_y) # 出現(xiàn)了幾個人,就有幾個box,就有幾個patch # todo yimage_patches_y_270.append(patch_y_270) # 出現(xiàn)了幾個人,就有幾個box,就有幾個patch # todo y270# type(image_patches) 是列表image_patches = np.asarray(image_patches) # image_patches --> <class 'numpy.ndarray'>image_patches_y = np.asarray(image_patches_y) # todo yimage_patches_y_270 = np.asarray(image_patches_y_270) # todo y270# type(image_patches)是numpy.ndarray數(shù)組# print('batch_size', batch_size) # 永遠(yuǎn)都是1# print('image_patches', image_patches)return image_encoder(image_patches, batch_size), \image_encoder(image_patches_y, batch_size),\image_encoder(image_patches_y_270, batch_size)return encoder# TODO 特征2: 原始的x方向單一特征 def create_box_encoder(model_filename, input_name="images",output_name="features", batch_size=32): # TODO 1 encoder(model_filename, batch_size=1)image_encoder = ImageEncoder(model_filename, input_name, output_name) # TODO ImageEncoder# image_encoder= out [[0.17.., ... ,0.066..][..., ..., ...]] (2,128) 數(shù)組含義,兩個人,128特征# print('image_encoder:', image_encoder)# image_encoder: <tools.generate_detections.ImageEncoder object at 0x7efc20031f98>image_shape = image_encoder.image_shape# print('image_shape:', image_shape)# image_shape: [128, 64, 3]# print('image_shape:', image_shape) # self.image_shape高寬為[128, 64, 3]def encoder(image, boxes):print('-->單特征create_box_encoder')image_patches = []# image_patches_y = [] # todo y# image_patches_y_270 = [] # todo y270for box in boxes: # 此處循環(huán), 針對每個目標(biāo)進(jìn)行處理patch = extract_image_patch(image, box, image_shape[:2]) # TODO 5 x 縮放法# patch_y = scaling_y_extract_image_patch(image, box, image_shape[:2]) # todo y# patch_y_270 = scaling_y_270_extract_image_patch(image, box, image_shape[:2]) # todo y270# TODO 5 extract_image_patch & scaling_x_extract_image_patch# print('格式:', type(patch)) # <class 'numpy.ndarray'># image_shape[:2]=[128, 64]# type(patch)是numpy.ndarray數(shù)組# TODO patch是正常的縱向reid,patch2,patch3是橫向if patch is None:print("WARNING: Failed to extract image patch: %s." % str(box))patch = np.random.uniform(0., 255., image_shape).astype(np.uint8) # 在0到255范圍內(nèi)隨機取,矩陣是[128,64]的隨機數(shù)'''if patch_y is None: # todo yprint("WARNING: Failed to extract image patch: %s." % str(box))patch_y = np.random.uniform(0., 255., image_shape).astype(np.uint8) # 在0到255范圍內(nèi)隨機取,矩陣是[128,64]的隨機數(shù)if patch_y_270 is None: # todo yprint("WARNING: Failed to extract image patch: %s." % str(box))patch_y_270 = np.random.uniform(0., 255., image_shape).astype(np.uint8) # 在0到255范圍內(nèi)隨機取,矩陣是[128,64]的隨機數(shù)'''image_patches.append(patch) # 出現(xiàn)了幾個人,就有幾個box,就有幾個patch <class 'list'># image_patches_y.append(patch_y) # 出現(xiàn)了幾個人,就有幾個box,就有幾個patch # todo y# image_patches_y.append(patch_y_270) # 出現(xiàn)了幾個人,就有幾個box,就有幾個patch # todo y270# type(image_patches) 是列表image_patches = np.asarray(image_patches) # image_patches --> <class 'numpy.ndarray'># image_patches_y = np.asarray(image_patches_y) # todo y# image_patches_y_270 = np.asarray(image_patches_y_270) # todo y270# type(image_patches)是numpy.ndarray數(shù)組# print('batch_size', batch_size) # 永遠(yuǎn)都是1# print('image_patches', image_patches)return image_encoder(image_patches, batch_size)# image_encoder(image_patches_y, batch_size), image_encoder(image_patches_y_270, batch_size)return encoderdef generate_detections(encoder, mot_dir, output_dir, detection_dir=None):"""Generate detections with features.Parameters----------encoder : Callable[image, ndarray] -> ndarrayThe encoder function takes as input a BGR color image and a matrix ofbounding boxes in format `(x, y, w, h)` and returns a matrix ofcorresponding feature vectors.mot_dir : strPath to the MOTChallenge directory (can be either train or test).output_dirPath to the output directory. Will be created if it does not exist.detection_dirPath to custom detections. The directory structure should be the defaultMOTChallenge structure: `[sequence]/det/det.txt`. If None, uses thestandard MOTChallenge detections."""if detection_dir is None:detection_dir = mot_dirtry:os.makedirs(output_dir)except OSError as exception:if exception.errno == errno.EEXIST and os.path.isdir(output_dir):passelse:raise ValueError("Failed to created output directory '%s'" % output_dir)for sequence in os.listdir(mot_dir):print("Processing %s" % sequence)sequence_dir = os.path.join(mot_dir, sequence)image_dir = os.path.join(sequence_dir, "img1")image_filenames = {int(os.path.splitext(f)[0]): os.path.join(image_dir, f)for f in os.listdir(image_dir)}detection_file = os.path.join(detection_dir, sequence, "det/det.txt")detections_in = np.loadtxt(detection_file, delimiter=',')detections_out = []frame_indices = detections_in[:, 0].astype(np.int)min_frame_idx = frame_indices.astype(np.int).min()max_frame_idx = frame_indices.astype(np.int).max()for frame_idx in range(min_frame_idx, max_frame_idx + 1):print("Frame %05d/%05d" % (frame_idx, max_frame_idx))mask = frame_indices == frame_idxrows = detections_in[mask]if frame_idx not in image_filenames:print("WARNING could not find image for frame %d" % frame_idx)continuebgr_image = cv2.imread(image_filenames[frame_idx], cv2.IMREAD_COLOR)features = encoder(bgr_image, rows[:, 2:6].copy())detections_out += [np.r_[(row, feature)] for row, featurein zip(rows, features)]output_filename = os.path.join(output_dir, "%s.npy" % sequence)np.save(output_filename, np.asarray(detections_out), allow_pickle=False)def parse_args():"""Parse command line arguments."""parser = argparse.ArgumentParser(description="Re-ID feature extractor")parser.add_argument("--model",default="resources/networks/mars-small128.pb",help="Path to freezed inference graph protobuf.")parser.add_argument("--mot_dir", help="Path to MOTChallenge directory (train or test)",required=True)parser.add_argument("--detection_dir", help="Path to custom detections. Defaults to ""standard MOT detections Directory structure should be the default ""MOTChallenge structure: [sequence]/det/det.txt", default=None)parser.add_argument("--output_dir", help="Output directory. Will be created if it does not"" exist.", default="detections")return parser.parse_args()def main():args = parse_args()encoder = create_box_encoder(args.model, batch_size=32)generate_detections(encoder, args.mot_dir, args.output_dir,args.detection_dir)if __name__ == "__main__":main()

?

?

?

?

?

?

?

?

?

?

?

?

總結(jié)

以上是生活随笔為你收集整理的RCAR会议:我的RTFA算法里面的generate_detections.py文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。