我们常常能接触到图像识别,例如人脸识别,物体检测,自动驾驶等等。让我们通过ImageAI来初探图像识别吧。
环境这里就不再赘述,请自觉查询官网
https://github.com/OlafenwaMoses/ImageAI
目录结构
$ tree .
.
├── 1.jpg
├── FirstPrediction.py
├── image2.jpg
└── resnet50-19c8e357.pth
0 directories, 4 files
代码
from imageai.Classification import ImageClassification
import os
execution_path = os.getcwd()
prediction = ImageClassification()
prediction.setModelTypeAsResNet50()
prediction.setModelPath(os.path.join(execution_path, "resnet50-19c8e357.pth"))
prediction.loadModel()
# predictions, probabilities = prediction.classifyImage(os.path.join(execution_path, "1.jpg"), result_count=5 )
predictions, probabilities = prediction.classifyImage(os.path.join(execution_path, "image2.jpg"), result_count=5 )
for eachPrediction, eachProbability in zip(predictions, probabilities):
print(eachPrediction , " : " , eachProbability)
输出
sports car : 65.5919
convertible : 12.3926
car wheel : 8.0013
beach wagon : 5.3135
grille : 4.7835
目录结构
$ tree
.
├── FirstObjectDetection.py
├── image2.jpg
├── image2new.jpg
└── yolov3.pt
0 directories, 4 files
代码
from imageai.Detection import ObjectDetection
import os
execution_path = os.getcwd()
detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath( os.path.join(execution_path, "yolov3.pt"))
detector.loadModel()
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path, "image2.jpg"), output_image_path=os.path.join(execution_path, "image2new.jpg"), minimum_percentage_probability=30)
for eachObject in detections:
print(eachObject["name"], " : ", eachObject["percentage_probability"], " : ", eachObject["box_points"])
print("--------------------------------")
目录结构
$ tree
.
├── FirstVideoObjectDetection.py
├── traffic.mp4
├── traffic_detected.mp4
└── yolov3.pt
0 directories, 4 files
代码
from imageai.Detection import VideoObjectDetection
import os
execution_path = os.getcwd()
detector = VideoObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath(os.path.join(execution_path, "yolov3.pt"))
detector.loadModel()
video_path = detector.detectObjectsFromVideo(input_file_path=os.path.join(execution_path, "traffic.mp4"),
output_file_path=os.path.join(execution_path, "traffic_detected")
, frames_per_second=20, log_progress=True)
print(video_path)