-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathimage_detection.py
106 lines (78 loc) · 2.54 KB
/
image_detection.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
import os
import cv2
import sys
import random
import math
import re
import time
import numpy as np
import tensorflow as tf
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import skimage
import glob
# Root directory of the project
ROOT_DIR = os.getcwd()
# Import Mask RCNN
sys.path.append(ROOT_DIR) # To find local version of the library
from mrcnn import utils
from mrcnn import visualize
from mrcnn.visualize import display_images
import mrcnn.model as modellib
from mrcnn.model import log
from mrcnn import parse_args
args = parse_args.parse_args()
import dataset
import training
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
weight_path = args.weight
config = training.CustomConfig()
dataset_dir = os.path.join(ROOT_DIR, args.dataset)
image_path = os.path.join(ROOT_DIR, args.image)
class InferenceConfig(config.__class__):
# Run detection on one image at a time
GPU_COUNT = 1
IMAGES_PER_GPU = 1
config = InferenceConfig()
config.display()
# Device to load the neural network on.
# Useful if you're training a model on the same
# machine, in which case use CPU and leave the
# GPU for training.
#DEVICE = "/gpu:0" # /cpu:0 or /gpu:0
# Load validation dataset
dataset = dataset.CustomDataset()
dataset.load_custom(dataset_dir, "val")
dataset.prepare()
print("Images: {}\nClasses: {}".format(len(dataset.image_ids), dataset.class_names))
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR,
config=config)
print("Loading weights ", weight_path)
model.load_weights(weight_path, by_name=True)
n = 0
positive = 0
negative = 0
for image_name in sorted(os.listdir(image_path)):
if image_name.endswith(('.jpg','.jpeg')):
image = skimage.io.imread(os.path.join(image_path, image_name))
if image.ndim != 3:
image = skimage.color.gray2rgb(image)
if image.shape[-1] == 4:
image = image[..., :3]
check = 0
results = model.detect([image], verbose=1)
r = results[0]
n += 1
if r["rois"].shape[0]:
positive += 1
check = 1
else: negative += 1
if check:
print(image_name," - Positive")
else: print(image_name," - Negative")
# visualize.display_instances(image_name,image, r['rois'], r['masks'], r['class_ids'], dataset.class_names, r['scores'])
visualize.save_image(image_name, image, r['rois'], r['masks'], r['class_ids'], dataset.class_names, r['scores'])
print("Total: ", n ," images")
print("Postivie: ", positive, " - ",positive/n * 100,"%")
print("Negative: ", negative, " - ",negative/n * 100,"%")