This time as data for deep learning Not only the datasets installed in Keras as standard, such as MNIST and Cifar10 Learn with Keras using images you download online.
For gender identification, the summary uses a dataset called lfs at http://vis-www.cs.umass.edu/lfw/.
Also for research purposes at https://data.vision.ee.ethz.ch/cvl/rrothe/imdb-wiki/ For example, the imdb dataset.
It also comes with metadata and is very easy to handle and is perfect for review.
First, load and display the image.
#Import CV2 and NumPy to use OpenCV. Also import Matplotlib for image display
import cv2
import matplotlib.pyplot as plt
import numpy as np
#Reading with OpenCV. Let's specify the path as an argument
img = cv2.imread('./6100_gender_recognition_data/female/Adriana_Lima_0001.jpg')
#Process the data read by OpenCV so that it can be displayed by matplotlib. b,g,Since it is in r order, r,g,Retransform to b
b,g,r = cv2.split(img)
img = cv2.merge([r,g,b])
#Output image using matplotlib
plt.imshow(img)
plt.show()
When doing machine learning, we rarely use images as they are.
If you capture an image that is too detailed, the learning time will be very long. Also, in general, not all images obtained from the Internet etc. are the same size.
Therefore, resize using the function of CV2.
import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread('./6100_gender_recognition_data/female/Adriana_Lima_0001.jpg')
#I will try to make it 50 x 50 size. Specify the image and dimension in the argument
my_img = cv2.resize(img, (50, 50))
#I'm using imwrite to save the image
cv2.imwrite('resize.jpg', my_img)
img = plt.imread('resize.jpg')
plt.imshow(img)
plt.show()
Color is represented by three elements: red, green, and blue.
In machine learning, this is often combined into one for convenience due to computer performance. In other words, it is expressed in black and white.
import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread('./6100_gender_recognition_data/female/Adriana_Lima_0001.jpg')
#You can convert to a monochrome image by setting the second argument of cvtColor to RGB2GRAY.
my_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
cv2.imwrite('cvtColor.jpg', my_img)
img = plt.imread('cvtColor.jpg')
plt.imshow(img)
#Matplotlib recognizes the first element as green, so use gray to display it in black and white.
plt.gray()
plt.show()
Review of basic operation of OpenCV
import cv2
import matplotlib.pyplot as plt
import numpy as np
img = cv2.imread('./6100_gender_recognition_data/female/Angelina_Jolie_0001.jpg')
my_img = cv2.resize(img, (50, 50))
my_img = cv2.cvtColor(my_img, cv2.COLOR_RGB2GRAY)
cv2.imwrite('final.jpg', my_img)
img = plt.imread('final.jpg')
plt.imshow(img)
plt.gray()
plt.show()
Recommended Posts