首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

春节出门又双叒叕找不到停车位?Python一招帮你告别行路难!

作者 | Adam Geitgey

译者 | 风车云马

整理 | Jane

临近春节,大家走亲访友外出游玩逛街,开车出门时最怕的就是找不到停车位。昨天小年,小编约了几个小伙伴去一家饺子馆吃饺子,结果七拐八拐,好不容易才找到一个停车位,然后又走了半天才回到饺子馆。

于是今天赶紧给大家推荐一个用Python找空车位的方法,这个春节就赶紧用上吧!(非程序猿可直达底部查看总结攻略)

本文通过相机结合深度学习算法,基于 Python 语言建立一个高精度的停车位的通知系统,每当有新停车位时就会发短信提醒。听起来好像很复杂,真的方便实用吗?但实际上所使用的工具都是现成的,只要将这些工具进行有机的组合,就可以快速、简便的实现。

下面我们就开始看看整个流程:

分解问题

解决一个复杂的问题,首先第一步是要把问题分解成几个简单子任务。然后,针对每个子任务,运用机器学习中不同的方法来分别解决每个问题。最后把这些子任务贯穿起来,形成整套解决方案。

下面是检测开放停车位的流程图:

输入:普通摄像头采集的视频流:

有了输入数据后,接下来我们需要知道图像的哪一部分是停车位,而且停车位是没有人使用的。

第一步:检测视频帧中所有可能的停车位。

第二步:检测每一帧视频中的所有车辆。可以跟踪每辆车从一帧到另一帧的运动。

第三步:确定目前有哪些停车位被占用,哪些没有被占用。这需要结合第一步和第二步的结果。

最后一步:当停车位变成可用时,系统发出通知。

其实可以使用多种不同的方法来完成这些步骤。不同的方法将具有不同的优势和缺点。接下来具体来看:

01

探测停车位

相机视图如下图所示:

需要扫描图像,并得到有效停车区域,如下面黄色标识出来的位置:

一个懒办法是程序写死每个停车场的位置,而不是自动检测停车场。但是如果移动摄像机,探测不同街道上的停车位,就必须再次手动定位停车位置。这样看来这个方法一点都不好,还是要采用自动检测停车位的方法。

其中一个想法是识别停车计时器并假设每个计时器旁边都有一个停车位:

但是这种方法也有一些问题。首先,并不是每个停车位有一个停车计时器,而且我们最想先找到免费停车位。第二,知道停车计时器的位置还不能告诉我们确切的停车位置点。

另一个想法是搭建一个目标检测模型,找到在马路上画的停车位记号,就像下图标识出来的:

这种方法也有两个难点。首先,从远处看,停车位线的标志很小,很难看到,增加了识别难度;其次,道路上还有各种交通标记线,比如车道线、人行道斑马线,这也给识别增加了难度。

或许还可以换个思维方式,回避一些技术挑战。重新思考停车位到底是什么呢?无非是一个车需要停放一定时间的位置。所以我们可能根本不需要检测停车位,只要检测出长时间不动的车,并假设它们的位置就是停车位。

所以,如果我们能检测出汽车,并找出哪些车在视频帧之间没有移动,就可以推断停车位的位置。

02

在图像中检测汽车

在视频中检测车辆是一个经典的目标检测问题。有很多机器学习方法可以实现。下面列出了一些最常见的目标检测算法:

1、通过 HOG(梯度方向直方图)目标检测器检测出所有的车。这种非深度学习方法运行起来相对较快,但它无法处理汽车在不同方向上的旋转问题。

2、通过 CNN(卷积神经网络)目标检测器检测所有的车。这种方法是准确的,但是效率比较低,因为同一张图像必须扫描多次,以检测到所有的汽车。虽然它可以很容易地对不同旋转方向的汽车定向,但是比 HOG 方法需要更多的训练数据。

3、使用新的深度学习方法,如 Mask R-CNN,Faster R-CNN 或者 YOLO 算法,兼容准确性和运行效率,大大加快了检测过程。一旦有大量的训练数据,在 GPU 上运行也很快。

通常来说,我们希望选择最简单可行的算法和最少的训练数据,而不是一定要用那些流行的新的算法。基于目前这种特殊场景下,Mask R- CNN 是一个不错的选择。

Mask R-CNN 架构就是在整个图像中检测对象,不使用滑动窗口的方式,所以运行速度很快。有了 GPU 处理器,我们能够每秒处理多帧的高分辨率视频,从中检测到车辆。

Mask R-CNN 为我们提供了很多检测到的信息。大多数目标检测算法只返回每个对象的边框。但是 Mask R-CNN 不仅会给我们每个对象的位置,也会给出一个对象的轮廓,像这样:

为了训练 Mask R-CNN 模型,我们需要很多这类检测物体的图片。可以花几天的时间出去拍摄照片,不过已经存在一些汽车图像的公共数据集。有一个很流行的数据集叫做COCO(Common Objects In Context的缩写),它里面已经有超过 12000 张汽车的图片。下面就是一个 COCO 数据集中的图像:

这些数据可以很好的训练 Mask R-CNN 模型,而且已经有很多人使用过 COCO数据集,并分享了训练的结果。所以我们可以直接使用一些训练好的模型,在本项目中使用 Matterport 的开源模型。

不仅能识别车辆,还能识别到交通灯和人。有趣的是,它把其中一棵树识别成“potted plant”。对于图像中检测到的每个对象,我们从 MaskR-CNN 模型得出以下 4 点:

(1)不同对象的类别,COCO 模型可以识别出 80 种不同的物体,比如小轿车和卡车。

(2)目标识别的置信度,数字越大,说明模型识别对象的精准度越高。

(3)图像中物体的边界框,给定了 X/Y 像素的位置。

(4)位图“mask”说明了边框内哪些像素是对象的一部分,哪些不是。使用“mask”数据,我们也可以算出物体的轮廓。

下面是 Python 代码,使用 Matterport 的 Mask R-CNN 的训练模型和 OpenCV 来检测汽车边框:

1importos

2importnumpyasnp

3importcv2

4importmrcnn.config

5importmrcnn.utils

6frommrcnn.modelimportMaskRCNN

7frompathlibimportPath

8

9

10# Configuration that will be used by the Mask-RCNN library

11classMaskRCNNConfig(mrcnn.config.Config):

12NAME ="coco_pretrained_model_config"

13IMAGES_PER_GPU =1

14GPU_COUNT =1

15NUM_CLASSES =1+80# COCO dataset has 80 classes + one background class

16DETECTION_MIN_CONFIDENCE =0.6

17

18

19# Filter a list of Mask R-CNN detection results to get only the detected cars / trucks

20defget_car_boxes(boxes, class_ids):

21car_boxes = []

22

23fori, boxinenumerate(boxes):

24# If the detected object isn't a car / truck, skip it

25ifclass_ids[i]in[3,8,6]:

26car_boxes.append(box)

27

28returnnp.array(car_boxes)

29

30

31# Root directory of the project

32ROOT_DIR = Path(".")

33

34# Directory to save logs and trained model

35MODEL_DIR = os.path.join(ROOT_DIR,"logs")

36

37# Local path to trained weights file

38COCO_MODEL_PATH = os.path.join(ROOT_DIR,"mask_rcnn_coco.h5")

39

40# Download COCO trained weights from Releases if needed

41ifnotos.path.exists(COCO_MODEL_PATH):

42mrcnn.utils.download_trained_weights(COCO_MODEL_PATH)

43

44# Directory of images to run detection on

45IMAGE_DIR = os.path.join(ROOT_DIR,"images")

46

47# Video file or camera to process - set this to 0 to use your webcam instead of a video file

48VIDEO_SOURCE ="test_images/parking.mp4"

49

50# Create a Mask-RCNN model in inference mode

51model = MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=MaskRCNNConfig())

52

53# Load pre-trained model

54model.load_weights(COCO_MODEL_PATH, by_name=True)

55

56# Location of parking spaces

57parked_car_boxes =None

58

59# Load the video file we want to run detection on

60video_capture = cv2.VideoCapture(VIDEO_SOURCE)

61

62# Loop over each frame of video

63whilevideo_capture.isOpened():

64success, frame = video_capture.read()

65ifnotsuccess:

66break

67

68# Convert the image from BGR color (which OpenCV uses) to RGB color

69rgb_image = frame[:, :, ::-1]

70

71# Run the image through the Mask R-CNN model to get results.

72results = model.detect([rgb_image], verbose=)

73

74# Mask R-CNN assumes we are running detection on multiple images.

75# We only passed in one image to detect, so only grab the first result.

76r = results[]

77

78# The r variable will now have the results of detection:

79# - r['rois'] are the bounding box of each detected object

80# - r['class_ids'] are the class id (type) of each detected object

81# - r['scores'] are the confidence scores for each detection

82# - r['masks'] are the object masks for each detected object (which gives you the object outline)

83

84# Filter the results to only grab the car / truck bounding boxes

85car_boxes = get_car_boxes(r['rois'], r['class_ids'])

86

87print("Cars found in frame of video:")

88

89# Draw each box on the frame

90forboxincar_boxes:

91print("Car: ", box)

92

93y1, x1, y2, x2 = box

94

95# Draw the box

96cv2.rectangle(frame, (x1, y1), (x2, y2), (,255,),1)

97

98# Show the frame of video on the screen

99cv2.imshow('Video', frame)

100

101# Hit 'q' to quit

102ifcv2.waitKey(1) &0xFF== ord('q'):

103break

104

105# Clean up everything when finished

106video_capture.release()

107cv2.destroyAllWindows()

运行该脚本后,将会看到在图像中识别到的汽车和边框:

同时会得到检测的每辆车的像素坐标:

这样我们已经成功地在图像中检测到了汽车。接下来到了下一个步骤。

03

探测空车位

知道图像中每辆车的像素位置后,通过观察连续多帧视频,可以很容易地算出哪帧里汽车没有移动。但我们如何检测到汽车何时离开停车位?经观察,图像中汽车的边框部分有所重叠:

如果假设每个边界框代表一个停车场空间,这个区域即使有车开走了,但是仍可能被另外汽车部分占据。因此我们需要一种方法来测量重叠,检查出“大部分为空”的框。我们使用的度量方法称为 Intersection Over Union(IoU)。通过计算两个物体重叠的像素量,然后除以两个物体所覆盖的像素:

有了这个值,接下来就可以很容易确定一辆车是否在停车位。如果 IoU 测量值低,比如 0.15,表示汽车并没有占据大部分的停车位空间。但是如果测量值很高,比如 0.6,就表示汽车占据了大部分的停车位,因此可以确定停车位已被占用。

结果显示为:

在二维数组中,每一行表示一个停车位边界框。同样的,每一列表示停车场被汽车所覆盖的程度。1.0 分意味着汽车完全占据了,而 0.02 这样的低分数,意味着有重叠区域,但不会占据大部分空间。

要找到无人使用的停车位,只需要计算出这个数组。如果所有的数都是 0 或者很小,也就表示空间没有被占用,因此一定是空停车位。

尽管 Mask R-CNN 非常精确,但目标检测并不能做到完美。有时也会在一段视频中漏掉一两辆车。所以在定位到一个空车位时,还应该检测在一段时间内都是空的,比如 5或10帧连续视频。这也可以避免视频本身出现故障而造成误检。一旦看到几个连续视频中都有空车位,马上发送提醒通知!

04

发送消息

最后一步是发送 SMS 提醒消息。利用 Twilio 通过 Python 发送 SMS 消息非常简单,基本上几行代码就可以实现。当然,Twilio 只是这个项目中用到的方法,你也可以用其他方式实现。

要使用 Twilio,先要注册一个试用帐户,创建一个 Twilio 电话号码并获取您的帐户凭证。然后,您需要安装 Twilio Python 客户端库:

下面是发送 SMS 消息的 Python 代码 (需用自己的帐户信息替换这些值):

在添加 SMS 发送功能时要注意,不要连续发送已经识别过的空车位信息。可以用一个 flag 来跟踪已经发过的短信,除非是设定一段时间后再次提醒或是检测到新的空车位。

05

把所有流程串在一起

现在将每个步骤集成一个Python脚本。下面是完整代码,要运行这段代码,需要安装Python 3.6+,Matterport 的 Mask R-CNN 和 OpenCV:

1importos

2importnumpyasnp

3importcv2

4importmrcnn.config

5importmrcnn.utils

6frommrcnn.modelimportMaskRCNN

7frompathlibimportPath

8fromtwilio.restimportClient

9

10

11# Configuration that will be used by the Mask-RCNN library

12classMaskRCNNConfig(mrcnn.config.Config):

13NAME ="coco_pretrained_model_config"

14IMAGES_PER_GPU =1

15GPU_COUNT =1

16NUM_CLASSES =1+80# COCO dataset has 80 classes + one background class

17DETECTION_MIN_CONFIDENCE =0.6

18

19

20# Filter a list of Mask R-CNN detection results to get only the detected cars / trucks

21defget_car_boxes(boxes, class_ids):

22car_boxes = []

23

24fori, boxinenumerate(boxes):

25# If the detected object isn't a car / truck, skip it

26ifclass_ids[i]in[3,8,6]:

27car_boxes.append(box)

28

29returnnp.array(car_boxes)

30

31

32# Twilio config

33twilio_account_sid ='YOUR_TWILIO_SID'

34twilio_auth_token ='YOUR_TWILIO_AUTH_TOKEN'

35twilio_phone_number ='YOUR_TWILIO_SOURCE_PHONE_NUMBER'

36destination_phone_number ='THE_PHONE_NUMBER_TO_TEXT'

37client = Client(twilio_account_sid, twilio_auth_token)

38

39

40# Root directory of the project

41ROOT_DIR = Path(".")

42

43# Directory to save logs and trained model

44MODEL_DIR = os.path.join(ROOT_DIR,"logs")

45

46# Local path to trained weights file

47COCO_MODEL_PATH = os.path.join(ROOT_DIR,"mask_rcnn_coco.h5")

48

49# Download COCO trained weights from Releases if needed

50ifnotos.path.exists(COCO_MODEL_PATH):

51mrcnn.utils.download_trained_weights(COCO_MODEL_PATH)

52

53# Directory of images to run detection on

54IMAGE_DIR = os.path.join(ROOT_DIR,"images")

55

56# Video file or camera to process - set this to 0 to use your webcam instead of a video file

57VIDEO_SOURCE ="test_images/parking.mp4"

58

59# Create a Mask-RCNN model in inference mode

60model = MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=MaskRCNNConfig())

61

62# Load pre-trained model

63model.load_weights(COCO_MODEL_PATH, by_name=True)

64

65# Location of parking spaces

66parked_car_boxes =None

67

68# Load the video file we want to run detection on

69video_capture = cv2.VideoCapture(VIDEO_SOURCE)

70

71# How many frames of video we've seen in a row with a parking space open

72free_space_frames =

73

74# Have we sent an SMS alert yet?

75sms_sent =False

76

77# Loop over each frame of video

78whilevideo_capture.isOpened():

79success, frame = video_capture.read()

80ifnotsuccess:

81break

82

83# Convert the image from BGR color (which OpenCV uses) to RGB color

84rgb_image = frame[:, :, ::-1]

85

86# Run the image through the Mask R-CNN model to get results.

87results = model.detect([rgb_image], verbose=)

88

89# Mask R-CNN assumes we are running detection on multiple images.

90# We only passed in one image to detect, so only grab the first result.

91r = results[]

92

93# The r variable will now have the results of detection:

94# - r['rois'] are the bounding box of each detected object

95# - r['class_ids'] are the class id (type) of each detected object

96# - r['scores'] are the confidence scores for each detection

97# - r['masks'] are the object masks for each detected object (which gives you the object outline)

98

99ifparked_car_boxesisNone:

100# This is the first frame of video - assume all the cars detected are in parking spaces.

101# Save the location of each car as a parking space box and go to the next frame of video.

102parked_car_boxes = get_car_boxes(r['rois'], r['class_ids'])

103else:

104# We already know where the parking spaces are. Check if any are currently unoccupied.

105

106# Get where cars are currently located in the frame

107car_boxes = get_car_boxes(r['rois'], r['class_ids'])

108

109# See how much those cars overlap with the known parking spaces

110overlaps = mrcnn.utils.compute_overlaps(parked_car_boxes, car_boxes)

111

112# Assume no spaces are free until we find one that is free

113free_space =False

114

115# Loop through each known parking space box

116forparking_area, overlap_areasinzip(parked_car_boxes, overlaps):

117

118# For this parking space, find the max amount it was covered by any

119# car that was detected in our image (doesn't really matter which car)

120max_IoU_overlap = np.max(overlap_areas)

121

122# Get the top-left and bottom-right coordinates of the parking area

123y1, x1, y2, x2 = parking_area

124

125# Check if the parking space is occupied by seeing if any car overlaps

126# it by more than 0.15 using IoU

127ifmax_IoU_overlap

128# Parking space not occupied! Draw a green box around it

129cv2.rectangle(frame, (x1, y1), (x2, y2), (,255,),3)

130# Flag that we have seen at least one open space

131free_space =True

132else:

133# Parking space is still occupied - draw a red box around it

134cv2.rectangle(frame, (x1, y1), (x2, y2), (,,255),1)

135

136# Write the IoU measurement inside the box

137font = cv2.FONT_HERSHEY_DUPLEX

138cv2.putText(frame,f"", (x1 +6, y2 -6), font,0.3, (255,255,255))

139

140# If at least one space was free, start counting frames

141# This is so we don't alert based on one frame of a spot being open.

142# This helps prevent the script triggered on one bad detection.

143iffree_space:

144free_space_frames +=1

145else:

146# If no spots are free, reset the count

147free_space_frames =

148

149# If a space has been free for several frames, we are pretty sure it is really free!

150iffree_space_frames >10:

151# Write SPACE AVAILABLE!! at the top of the screen

152font = cv2.FONT_HERSHEY_DUPLEX

153cv2.putText(frame,f"SPACE AVAILABLE!", (10,150), font,3.0, (,255,),2, cv2.FILLED)

154

155# If we haven't sent an SMS yet, sent it!

156ifnotsms_sent:

157print("SENDING SMS!!!")

158message = client.messages.create(

159body="Parking space open - go go go!",

160from_=twilio_phone_number,

161to=destination_phone_number

162)

163sms_sent =True

164

165# Show the frame of video on the screen

166cv2.imshow('Video', frame)

167

168# Hit 'q' to quit

169ifcv2.waitKey(1) &0xFF== ord('q'):

170break

171

172# Clean up everything when finished

173video_capture.release()

174cv2.destroyAllWindows()

这段代码写的比较简洁,实现了基本的功能。大家也可以试着修改代码以适应不同的场景。仅仅更改一下模型参数,出现的效果就可能完全不同,在不同应用中尽情发挥想象力!

https://medium.com/@ageitgey/snagging-parking-spaces-with-mask-r-cnn-and-python-955f2231c400

不会Python一头雾水?

没关系,

直接发给身边正在学Python的小伙伴,

课后练习了解一下

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20190129B0BVDS00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券