A story that I wanted to realize the identification of parking lot fullness information using images obtained with a Web camera and Raspberry Pi and deep learning.

Introduction

I took a video of the parking lot in front of me from the window of the office every 5 minutes and made a video to play with.

Youtube-Weekly Office Window

At the beginning, it was just fun, but when I was thinking, "I wonder if there is anything else I can do," I came up with something like the title.

I tried to make this result always available on the website. As mentioned above, the photos are updated every 5 minutes.

Sample parking lot condition monitoring

Library to use

Install the library to use.

OpenCV

pip install -U pip
pip install python-opencv

Tensorflow

pip install tensorflow

Keras

pip install keras

Preparation

First, create the following folders.

├ img
│ ├ 0-Many
│ ├ 1-Few
│ └ 2-rattle
└ models

Next, save the images you want to classify in each folder under img. This time, we used 2019 images with image data for about one week.

Many

202010111320.jpg 202010110035.jpg

Few

202010111210.jpg 202010170140.jpg

rattle

202010120740.jpg 202010112230.jpg

Learning

Learn images according to folder classification.

python


#Library read
import glob
import cv2
from matplotlib import pyplot as plt
import numpy as np

import keras
import tensorflow as tf

from sklearn import model_selection
from keras.utils.np_utils import to_categorical
from keras.layers import Activation, Conv2D, Dense, Flatten, MaxPooling2D, Dropout
from keras.models import Sequential

import random

#Label data creation
labels = []
for i in range(3):
    labels.append("{}-".format(i))
    
#Get the number of image files
n = []

for l in labels:
    files = glob.glob("img/{}*/*.jpg ".format(l))
    print("{} : {}".format(l, len(files)))
    
    n.append(len(files))

#Image file reading
imgX = []
y = []

k = 0

for l in labels:
    
    print(l)
    
    files = glob.glob("img/{}*/*.jpg ".format(l))
    files.sort()
    print(len(files), end=" -> ")
    
    j = int(min(n) * 1.5)
    if j > len(files):
        j = len(files)
    
    files = random.sample(files, j)
    print(len(files))
    
    i = 0

    for f in files:
        img = cv2.imread(f)
        h, w, c = img.shape
        img = img[int(h/2):h, :]
        img = cv2.resize(img, (100, 100))
        imgX.append(img)
        y.append(k)
        
        print("\r{}".format(i), end="")

        i += 1

    print()
    k += 1

#Convert image data to array data
X = np.array(imgX)
X = X / 255

#Divided into learning / verification data
test_size = 0.2

X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=test_size, random_state=42)
X_valid, X_test, y_valid, y_test = model_selection.train_test_split(X_test, y_test, test_size=.5, random_state=42)

y_train = to_categorical(y_train)
y_valid = to_categorical(y_valid)
y_test = to_categorical(y_test)

#Creating a learning model
input_shape = X[0].shape

model = Sequential()

model.add(Conv2D(
    input_shape=input_shape, filters=64, kernel_size=(5, 5), 
    strides=(1, 1), padding="same", activation='relu'))

model.add(MaxPooling2D(pool_size=(4, 4)))

model.add(Conv2D(
    filters=32, kernel_size=(5, 5), 
    strides=(1, 1), padding="same", activation='relu'))

model.add(Conv2D(
    filters=32, kernel_size=(5, 5), 
    strides=(1, 1), padding="same", activation='relu'))

model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(
    filters=16, kernel_size=(5, 5), 
    strides=(1, 1), padding="same", activation='relu'))

model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())
model.add(Dense(1024, activation='sigmoid'))
model.add(Dense(2048, activation='sigmoid'))

model.add(Dense(len(labels), activation='softmax'))

model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])

#Learning
history = model.fit(
    X_train, y_train, batch_size=400, epochs=200, 
    verbose=1, shuffle=True,
    validation_data=(X_valid, y_valid))

score = model.evaluate(X_test, y_test, batch_size=32, verbose=0)
print('validation loss:{0[0]}\nvalidation accuracy:{0[1]}'.format(score))

#Save trained model
mdlname = "models/mdl_parking_status.h5"
model.save(mdlname)

The output in the middle is omitted, and the learning results are as follows.

validation loss:0.15308915078639984
validation accuracy:0.9653465151786804

The graph below shows the learning process.

loss.png acc.png

I think I can learn somehow.

identification

Identified by the following script.

python


#Library read
import glob
import cv2
from matplotlib import pyplot as plt
import numpy as np
import requests
import keras

#Loading trained model
mdlname = "models/mdl_parking_status.h5"
model = keras.models.load_model(mdlname)

#Creating label data
labels = []

for lbl in glob.glob("img/*"):
    labels.append(lbl.split("/")[-1])

labels.sort()

#Image reading
img_url = "https://map.blueomega.jp/parking/img.jpg "
req = requests.get(img_url)
img_org = np.fromstring(req.content, dtype='uint8')
img_org = cv2.imdecode(img_org, 1)

h, w, c = img_org.shape
img = img_org[int(h/2):h, :]
img = cv2.resize(img, (100, 100))
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)

X = np.array([img])
X = X / 255

#identification
pred = model.predict(X, batch_size=32)
m = np.argmax(pred[0])

#Result display
print(pred)
print(labels[m])

Click here for the acquired image.

sample.jpg

The identification results are as follows.

[[9.2753559e-01 7.2361618e-02 1.0272356e-04]]
0-Many

... maybe done!

Recommended Posts

A story that I wanted to realize the identification of parking lot fullness information using images obtained with a Web camera and Raspberry Pi and deep learning.
I tried to make a motion detection surveillance camera with OpenCV using a WEB camera with Raspberry Pi
I tried the common story of using Deep Learning to predict the Nikkei 225
I wanted to run the motor with Raspberry Pi, so I tried using Waveshare's Motor Driver Board
Create a web surveillance camera with Raspberry Pi and OpenCV
I captured the Touhou Project with Deep Learning ... I wanted to.
A story that I had a hard time trying to create an "app that converts images like paintings" with the first web application
Note that I was addicted to accessing the DB with Python's mysql.connector using a web application.
Real-time classification of multiple objects in the camera image with deep learning of Raspberry Pi 3 B + & PyTorch
I made a web server with Raspberry Pi to watch anime
I made an npm package to get the ID of the IC card with Raspberry Pi and PaSoRi
I tried to extract and illustrate the stage of the story using COTOHA
The story of making a sound camera with Touch Designer and ReSpeaker
I tried the common story of predicting the Nikkei 225 using deep learning (backtest)
Gently explain the process of making a simple serverless surveillance camera using Raspberry Pi, Gmail API and Line API
Connect to the console of Raspberry PI and display local IP and SD information
The story of IPv6 address that I want to keep at a minimum
I tweeted the illuminance of the room with Raspberry Pi, Arduino and optical sensor
The story of making a web application that records extensive reading with Django
Build a python environment to learn the theory and implementation of deep learning
How to make a Raspberry Pi that speaks the tweets of the specified user
I made a system with Raspberry Pi that regularly measures the discomfort index of the room and sends a LINE notification if it is a dangerous value
The story of doing deep learning with TPU
A story about getting the Atom field (XML telegram) of the Japan Meteorological Agency with Raspberry Pi and tweeting it
I attached a camera to the Raspberry Pi and built a home monitoring site (but can only be viewed within my home Wifi area) using Motion software.
I tried to notify the update of "Become a novelist" using "IFTTT" and "Become a novelist API"
The story of Linux that I want to teach myself half a year ago
I want to collect a lot of images, so I tried using "google image download"
I made a web application that maps IT event information with Vue and Flask
I just wanted to extract the data of the desired date and time with Django
When writing to a csv file with python, a story that I made a mistake and did not meet the delivery date
Create a color sensor using a Raspberry Pi and a camera
Display images taken with the Raspberry Pi camera module
Collection and automation of erotic images using deep learning
A server that returns the number of people in front of the camera with bottle.py and OpenCV
Make a note of what you want to do in the future with Raspberry Pi
Easily make a TweetBot that notifies you of temperature and humidity with Raspberry Pi + DHT11.
I tried to compare the accuracy of machine learning models using kaggle as a theme.
A story that makes it easy to estimate the living area using Elasticsearch and Python
[Raspberry Pi] Scraping of web pages that cannot be obtained with python requests + Beautiful Soup
I tried to move ROS (Melodic) with the first Raspberry Pi (Stretch) at the beginning of 2021
I tried to get Web information using "Requests" and "lxml"
I tried connecting Raspberry Pi and conect + with Web API
I tried using the DS18B20 temperature sensor with Raspberry Pi
I tried to automate [a certain task] using Raspberry Pi
I made a surveillance camera with my first Raspberry PI.
I tried to divide with a deep learning language model
I sent the data of Raspberry Pi to GCP (free)
I tried to make a site that makes it easy to see the update information of Azure
A story that didn't work when I tried to log in with the Python requests module
I want to extract the tag information (title and artist) of a music file (flac, wav).