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.
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
Install the library to use.
OpenCV
pip install -U pip
pip install python-opencv
Tensorflow
pip install tensorflow
Keras
pip install keras
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.
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.
I think I can learn somehow.
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.
The identification results are as follows.
[[9.2753559e-01 7.2361618e-02 1.0272356e-04]]
0-Many
... maybe done!
Recommended Posts