forked from sahibdhanjal/Mask-RCNN-Pedestrian-Detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maskRCNN_USC.py
135 lines (116 loc) · 4.89 KB
/
maskRCNN_USC.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import os
import sys
import random
import math
import numpy as np
import skimage.io
import matplotlib
import matplotlib.pyplot as plt
# RCNN Dependencies
import utils
import coco
import utils
import model as modellib
import visualize
# Video Detection Dependenices
import cv2
import time
import urllib.request as urllib2
import USC_evaluate
import globalvar as gl
# Root directory of the project
ROOT_DIR = os.getcwd()
# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
# Local path to trained weights file
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
# Download COCO trained weights from Releases if needed
if not os.path.exists(COCO_MODEL_PATH):
utils.download_trained_weights(COCO_MODEL_PATH)
# Directory of images to run detection on
IMAGE_DIR = os.path.join(ROOT_DIR, "images")
OUTPUT_DIR = os.path.join(ROOT_DIR,"results_output")
class InferenceConfig(coco.CocoConfig):
# Set batch size to 1 since we'll be running inference on
# one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
GPU_COUNT = 1
IMAGES_PER_GPU = 1
config = InferenceConfig()
config.display()
# Create model object in inference mode.
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)
# Load weights trained on MS-COCO
model.load_weights(COCO_MODEL_PATH, by_name=True)
# COCO Class names
# Index of the class in the list is its ID. For example, to get ID of
# the teddy bear class, use: class_names.index('teddy bear')
class_names = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',
'bus', 'train', 'truck', 'boat', 'traffic light',
'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird',
'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear',
'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie',
'suitcase', 'frisbee', 'skis', 'snowboard', 'sports ball',
'kite', 'baseball bat', 'baseball glove', 'skateboard',
'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup',
'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple',
'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza',
'donut', 'cake', 'chair', 'couch', 'potted plant', 'bed',
'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote',
'keyboard', 'cell phone', 'microwave', 'oven', 'toaster',
'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors',
'teddy bear', 'hair drier', 'toothbrush']
def getAllFiles(dirName, houzhui):
results = []
for file in os.listdir(dirName):
file_path = os.path.join(dirName, file)
if os.path.isfile(file_path) and os.path.splitext(file_path)[1] == houzhui:
results.append([file_path,os.path.splitext(file)[0]])
return results
def load_image(imagePath):
"""Load the specified image and return a [H,W,3] Numpy array.
"""
# Load image
from PIL import Image
from PIL import ImageFile
import imghdr
ImageFile.LOAD_TRUNCATED_IMAGES = True
if imghdr.what(imagePath) == "png":
Image.open(imagePath).convert("RGB").save(imagePath)
image = cv2.imread(imagePath)
return image
t_prediction = 0
t_start = time.time()
imageNum = 0
image_USC_dir = os.path.join(ROOT_DIR,"USC","USCPedestrianSetC","images")
annotations_USC_dir = os.path.join(ROOT_DIR,"USC","USCPedestrianSetC","annotations")
imageNames = getAllFiles(image_USC_dir, '.bmp')
person_info = USC_evaluate.get_person_num_info(annotations_USC_dir)
all_person_num = 0
f = open("results.txt","w")
for imageName in imageNames:
frame =load_image(imageName[0])
#frame = cv2.imread('test/1.jpg')
##################################################
# Mask R-CNN Detection
##################################################
t = time.time()
results = model.detect([frame], verbose=0)
r = results[0]
t_prediction += (time.time() - t)
imageNum += 1
print('##########################')
print("imageName:{}.imageNum:{}.".format(imageName[1],imageNum))
##################################################
# Image Plotting
##################################################
visualize.display_instances(frame, r['rois'], r['masks'],
r['class_ids'], class_names,
r['scores'],title=imageName[1],
filepath= OUTPUT_DIR)
USC_evaluate.get_precision(imageName[1], r['class_ids'], person_info[imageName[1]],f)
print("Prediction time: {}. Average {}/image".format(
t_prediction, t_prediction / imageNum),file=f)
print("Total time:{} ".format(time.time() - t_start),file=f)
print("ground truth行人的总数量:{}".format(gl.get_value('total_person_num_gt')),file=f)
print("检测到行人的总数量:{}".format(gl.get_value('total_person_num_detect')),file=f)
f.close()