版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、OpenCV 計(jì)算機(jī)視覺(jué)實(shí)戰(zhàn),停車場(chǎng)車位識(shí)別?。ㄍ暾a)!src1:圖1 src2:圖2 mask:圖1和圖2與操作的掩碼輸出圖像def select_rgb_white_yellow(self,image):masked = cv2.bitwise_and(image, image, mask = white_mask)self.cv_show(masked,masked)def convert_gray_scale(self,image):cv2.circle(img,中點(diǎn)(x,y),半徑r,color,粗細(xì)) 根據(jù)給定的圓和半徑等畫圓 畫出指定點(diǎn)def filter_region(se
2、lf,image, vertices):剔除掉不需要的地vertices = np.array(pt_1, pt_2, pt_3, pt_4, pt_5, pt_6, dtype=32)point_img = image.copy()point_img = cv2.cvtColor(point_img, cv2.COLOR_GRAY2RGB)for point in vertices0:return self.filter_region(image, vertices)abs(y2-y1) =25 and abs(x2-x1) = 55 長(zhǎng)度太長(zhǎng)的也不要def hough_line
3、s(self,image):# 輸?shù)膱D像需要是邊緣檢測(cè)后的結(jié)果if make_copy:image = np.copy(image)cleaned = for line in lines:for x1,y1,x2,y2 in line:step 3:指定間距于10的,劃分為不同的列,共12簇def identify_blocks(self,image, lines, make_copy=True):if make_copy:if abs(y2-y1) =25 and abs(x2-x1) = 55:cleaned.append(x1,y1,x2,y2)clusters = dIndex = 0
4、distance = abs(list1i+10 - list1i0)if distance = clus_dist:if not dIndex in clusters.keys(): clustersdIndex = clustersdIndex.append(list1i)clustersdIndex.append(list1i + 1)else:dIndex += 1#Step 4: 得到坐標(biāo)rects = i = 0for key in clusters:avg_x2 = 0for tup in cleaned:avg_x1 += tup0avg_x2 += tup2buff = 7f
5、or key in rects:tup_topLeft = (int(rectskey0 - buff), int(rectskey1)tup_botRight = (int(rectskey2 + buff), int(rectskey3)cv2.rectangle(new_image, tup_topLeft,tup_botRight,(0,255,0),3)return new_image, rectsspot_dict = # 字典:個(gè)車位對(duì)應(yīng)個(gè)位置adj_x1 = 0: -8, 1:-15, 2:-15, 3:-15, 4:-15, 5:-15, 6:-15, 7:-15, 8:-1
6、0, 9:-10, 10:-10, 11:0adj_x2 = 0: 0, 1: 15, 2:15, 3:15, 4:15, 5:15, 6:15, 7:15, 8:10, 9:10, 10:10, 11:0# 繼續(xù)微調(diào)cv2.rectangle(new_image, (x1, y1),(x2,y2),(0,255,0),2)# (y2-y1)/gap表能停多少輛車num_splits = int(abs(y2-y1)/gap)for i in range(0, num_splits+1):y = int(y1 + i*gap)x = int(x1 + x2)/2)if key = 0 or k
7、ey = (len(rects) -1):tot_spots += num_splits +1else:tot_spots += 2*(num_splits +1) # 2# 字典對(duì)應(yīng)好if key = 0 or key = (len(rects) -1):for i in range(0, num_splits+1):cur_len = len(spot_dict)y = int(y1 + i*gap)x = int(x1 + x2)/2)filename = with_parking.jpgcv2.imwrite(filename, new_image)return new_image,
8、spot_dictsave_images_for_cnn 保存所有切割出來(lái)的圖必須的步驟,主要是要獲得車位坐標(biāo)的字典def save_images_for_cnn(self,image, spot_dict, folder_name =cnn_data):for spot in spot_dict.keys():filename = spot + str(spot_id) +.jpgprint(spot_img.shape, filename, (x1,x2,y1,y2)cv2.imwrite(os.path.join(folder_name, filename), spot_img)fina
9、l_spot_dict 是img_process函數(shù) return的車位坐標(biāo)字典if _name_ = _main_:class_dictionary0 = emptyclass_dictionary1 = occupiedpark = Parking() # Parking對(duì)象park.show_images(test_images)final_spot_dict = img_process(test_images,park) # 圖像處理model = keras_model(weights_path)img_test(test_images,final_spot_dict,model,c
10、lass_dictionary)video_test(video_name,final_spot_dict,model,class_dictionary)from keras.applications.imagenet_utils import preprocess_inputfrom keras.models import load_modelfrom keras.preprocessing import imagefrom Parking import Parkingimport picklecwd = os.getcwd()def img_process(test_images,park
11、):white_yellow_images = list(map(park.select_rgb_white_yellow, test_images)park.show_images(white_yellow_images)list_of_lines = list(map(park.hough_lines, roi_images)line_images = rect_images = rect_coords = # 區(qū)域置空spot_pos = for image, rects in zip(test_images, rect_coords):for image, rects in zip(t
12、est_images, rect_coords):new_image, spot_dict = park.draw_parking(image, rects)delineated.append(new_image)pickle.dump(final_spot_dict, handle, protocol=pickle.HIGHEST_PROTOCOL)# park.save_images_for_cnn(test_images0,final_spot_dict)model = load_model(weights_path)def img_test(test_images,final_spot
13、_dict,model,class_dictionary):for i in range (len(test_images):class_dictionary0 = emptyclass_dictionary1 = occupiedpark = Parking() # Parking對(duì)象park.show_images(test_images)final_spot_dict = img_process(test_images,park) # 圖像處理model = keras_model(weights_path)1234567891011121314151617181920212223242
14、52627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384import os, globrows = (len(images)+1)/colsplt.figure(figsize=(15, 12)for i, image in enumerate(images):plt.subplot(rows, cols, i+1)cmap = gray if len(image.shape)=2 else cmapplt.ims
15、how(image, cmap=cmap)plt.xticks()plt.tight_layout(pad=0, h_pad=0, w_pad=0)plt.show()cv2.waitKey(0)def select_rgb_white_yellow(self,image):# 過(guò)濾掉背景l(fā)ower = np.uint8(120, 120, 120)masked = cv2.bitwise_and(image, image, mask = white_mask)self.cv_show(masked,masked)def convert_gray_scale(self,image):def f
16、ilter_region(self,image, vertices):動(dòng)選擇區(qū)域# first, define the polygon by verticesrows, cols = image.shape:2pt_1 = cols*0.05, rows*0.90pt_2 = cols*0.05, rows*0.70pt_3 = cols*0.30, rows*0.55pt_4 = cols*0.6, rows*0.15pt_5 = cols*0.90, rows*0.15pt_6 = cols*0.90, rows*0.90vertices = np.array(pt_1, pt_2, pt
17、_3, pt_4, pt_5, pt_6, dtype=32)point_img = image.copy()return self.filter_region(image, vertices)def hough_lines(self,image):# 輸?shù)膱D像需要是邊緣檢測(cè)后的結(jié)果for line in lines:if abs(y2-y1) =25 and abs(x2-x1) = 55:cleaned.append(x1,y1,x2,y2)cv2.line(image, (x1, y1), (x2, y2), color, thickness)print( No lines
18、detected: , len(cleaned)return imagedef identify_blocks(self,image, lines, make_copy=True):if make_copy:new_image = np.copy(image)#Step 1: 過(guò)濾部分直線if abs(y2-y1) =25 and abs(x2-x1) = 55:cleaned.append(x1,y1,x2,y2)#Step 2: x1進(jìn)排序import operatordistance = abs(list1i+10 - list1i0)if distance = clus_dist:if
19、 not dIndex in clusters.keys(): clustersdIndex = clustersdIndex.append(list1i)clustersdIndex.append(list1i + 1)else:dIndex += 1#Step 4: 得到坐標(biāo)rects = i = 0for key in clusters:avg_x2 = 0for tup in cleaned:avg_x1 += tup0avg_x2 += tup2tup_topLeft = (int(rectskey0 - buff), int(rectskey1)tup_botRight = (in
20、t(rectskey2 + buff), int(rectskey3)cv2.rectangle(new_image, tup_topLeft,tup_botRight,(0,255,0),3)return new_image, rectscv2.rectangle(new_image, (x1, y1),(x2,y2),(0,255,0),2)# (y2-y1)/gap 表能停多少輛車num_splits = int(abs(y2-y1)/gap)for i in range(0, num_splits+1):y = int(y1 + i*gap)x = int(x1 + x2)/2)els
21、e:tot_spots += 2*(num_splits +1) # 2# 字典對(duì)應(yīng)好x = int(x1 + x2)/2)filename = with_parking.jpgcv2.imwrite(filename, new_image)return new_image, spot_dictnew_image = np.copy(image)for spot in spot_dict.keys():def save_images_for_cnn(self,image, spot_dict, folder_name =cnn_data):for spot in spot_dict.keys(
22、):def make_prediction(self,image,model,class_dictionary):image = np.expand_dims(img, axis=0)class_predicted = model.predict(image)new_image = np.copy(image)overlay = np.copy(image)self.cv_show(new_image,new_image)cnt_empty = 0all_spots = 0(x1, y1, x2, y2) = spotlabel = self.make_prediction(spot_img,model,class_dictionary)if label = empty:cv2.addWeighted(overlay, alpha, new_image, 1 - alpha, 0, new_image)cv2.putText(new_image, Available: %d spots %cnt_empty, (30, 95),cv2.FONT_HERSHEY_SIMPLEX,0.7, (255, 255, 255), 2)cv2.putText(new_image, Total: %d spots %all_spots, (30, 12
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 關(guān)于臨時(shí)簽訂合同報(bào)告
- 國(guó)企勞動(dòng)派遣合同
- 合同法案例精解
- 鐘點(diǎn)工聘用合同范本
- 大班課件《誰(shuí)是采蜜冠軍》
- 2024正規(guī)的自然人借款合同樣本
- 2024合同信息化管理系統(tǒng)【信息系統(tǒng)合同】
- 2024個(gè)人租房協(xié)議書合同租房協(xié)議書(詳細(xì)版)
- 2024標(biāo)準(zhǔn)銷售業(yè)務(wù)員合同范本
- 2024個(gè)體借款合同協(xié)議模板
- 2024年居間服務(wù)委托協(xié)議
- 人工智能訓(xùn)練師(中級(jí)數(shù)據(jù)標(biāo)注員)理論考試題庫(kù)大全(含答案)
- 手機(jī)綜合癥小品臺(tái)詞
- 建筑項(xiàng)目安全風(fēng)險(xiǎn)分級(jí)管控清單(范例)
- 客戶信息登記表
- 二手車復(fù)習(xí)題終極版本
- 畢業(yè)設(shè)計(jì)說(shuō)明書螺旋精確稱重給料機(jī)設(shè)計(jì)
- 十大危險(xiǎn)作業(yè)
- 組織文化研究文獻(xiàn)綜述
- 停電施工專項(xiàng)施工方案(π接
- 高三地理一模成績(jī)分析
評(píng)論
0/150
提交評(píng)論