OpenCV计算机视觉实战停车场车位识别!(完整代码)_第1页
OpenCV计算机视觉实战停车场车位识别!(完整代码)_第2页
OpenCV计算机视觉实战停车场车位识别!(完整代码)_第3页
OpenCV计算机视觉实战停车场车位识别!(完整代码)_第4页
OpenCV计算机视觉实战停车场车位识别!(完整代码)_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、OpenCV 计算机视觉实战,停车场车位识别!(完整代码)!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,中点(x,y),半径r,color,粗细) 根据给定的圆和半径等画圆 画出指定点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 长度太长的也不要def hough_line

3、s(self,image):# 输的图像需要是边缘检测后的结果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: 得到坐标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 = # 字典:个车位对应个位置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# 继续微调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# 字典对应好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 保存所有切割出来的图必须的步骤,主要是要获得车位坐标的字典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函数 return的车位坐标字典if _name_ = _main_:class_dictionary0 = emptyclass_dictionary1 = occupiedpark = Parking() # Parking对象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 = # 区域置空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对象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):# 过滤掉背景lower = 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):动选择区域# 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):# 输的图像需要是边缘检测后的结果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: 过滤部分直线if abs(y2-y1) =25 and abs(x2-x1) = 55:cleaned.append(x1,y1,x2,y2)#Step 2: x1进排序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: 得到坐标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# 字典对应好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. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论