Example #1
0
bb = 0
color_index = {
    'bus': 'red',
    'truck': 'indigo',
    'motorcycle': 'azure',
    'refrigerator': 'gold',
    'bicycle': 'olivedrab',
    'car': 'silver',
    'traffic light': 'mediumblue',
    'person': 'honeydew',
    'stop sign': 'beige'
}

cap = cv2.VideoCapture("rtsp://*****:*****@10.0.231.60")

detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("yolo.h5")
custom = detector.CustomObjects(bus=True,
                                truck=True,
                                motorcycle=True,
                                refrigerator=True,
                                bicycle=True,
                                car=True,
                                traffic_light=True,
                                person=True,
                                stop_sign=True)
detector.loadModel()

#cap = cv2.VideoCapture(0)
cap.set(3, 1280)
Example #2
0

def crop(image_path, coords, saved_location):
    """
    @param image_path: The path to the image to edit
    @param coords: A tuple of x/y coordinates (x1, y1, x2, y2)
    @param saved_location: Path to save the cropped image
    """
    image_obj = Image.open(image_path)
    cropped_image = image_obj.crop(coords)
    cropped_image.save(saved_location)


### load model for detecting persons in pictures
execution_path = os.getcwd()
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath(
    os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel()

### crop out images of people and sort based on progress pic metadata

# data_directory = 'cleaned_images'
data_directory = 'holdout_set'
written = 0
for f in glob.glob('progresspics/*.jpeg'):
    data = df.loc['progresspics/' + df['file_name'] == f, :]
    if len(data) != 1 or (data['start_weight'].isnull().any()) or (
            data['end_weight'].isnull().any()):
        continue
Example #3
0
# imports
from cv2 import *
from imageai.Detection import ObjectDetection
import PIL
from PIL import Image

# initialize webcam/video-cap & obj. detection model
cam = VideoCapture(0)
detector = ObjectDetection()
detector.setModelTypeAsTinyYOLOv3()
detector.setModelPath('yolo-tiny.h5')
detector.loadModel(detection_speed="fastest")


# get current frame & detect people
def see_people():
    ret, img = cam.read()
    imwrite("currentframe.png", img)
    detections = detector.detectObjectsFromImage(
        input_image="currentframe.png", output_image_path="newframe.png")
    # sort detected objects into "persons", with probability 60%+
    detected_people = []
    for eachObject in detections:
        if eachObject["name"] == "person" and eachObject[
                "percentage_probability"] >= 60:
            detected_people.append(eachObject)
    return detected_people


# use eyes
def eyes():
Example #4
0
import warnings
import pathlib
warnings.filterwarnings(action="ignore", message="^internal gelsd")

from imageai.Detection import ObjectDetection
import os
import pickle
import numpy as np
model = pickle.load(open('modelGud.pkl', 'rb'))
detector = ObjectDetection()
detector.setModelTypeAsTinyYOLOv3()
detector.setModelPath('./yolo-tiny.h5')
detector.loadModel()
custom = detector.CustomObjects(car=True)
path = "./electricStations"
evList = []
for i in os.listdir(path):
    if i.endswith('jpg'):
        evList.append(i)


def customPrice(electricStation):
    evPath = './electricStations/' + electricStation
    detections = detector.detectCustomObjectsFromImage(
        custom_objects=custom,
        input_image=evPath,
        output_image_path='./output/' + electricStation + '.jpg',
        minimum_percentage_probability=30)
    numberOfCars = 0
    for car in detections:
        numberOfCars += 1
Example #5
0
from imageai.Detection import ObjectDetection
import os

execution_path = os.getcwd()


detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath( os.path.join(execution_path , "yolo.h5"))
detector.loadModel()


detections, objects_path = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "image3.jpg"), output_image_path=os.path.join(execution_path , "image3new.jpg"), minimum_percentage_probability=30,  extract_detected_objects=True)


for eachObject, eachObjectPath in zip(detections, objects_path):
print(eachObject["name"] , " : " , eachObject["percentage_probability"], " : ", eachObject["box_points"] )
print("Object's image saved in " + eachObjectPath)
print("--------------------------------")

Example #6
0
#CL EYE CAM3 - 7 sek
#многострочные комменты - CTRL + /
import speech_recognition as sr
import cv2
import serial
import numpy as np
from imageai.Detection import ObjectDetection
import os
d=""
p=0
l=[]
# NOTE: ЗАГРУЗКА
execution_path = os.getcwd()
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path , "C:\\Users\\Admin_Robo\\Desktop\\project\\models\\resnet50_coco_best_v2.0.1.h5"))
detector.loadModel() ## NOTE: Для детекта минимального [незначительного] объекта - режим стандарт (7 sek ;()
print('LOADED')
ser = serial.Serial("COM8", 9600)
print('SERIAL KAROCHE')
while True:
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("говори")
        audio = r.listen(source,timeout=7)
    try:
        print("[СКАЗАЛ]: " + r.recognize_google(audio, language='RU-ru').lower())
        if r.recognize_google(audio, language='RU-ru').lower().find('дай') != -1:
            print('It works! #1')
            if r.recognize_google(audio, language='RU-ru').lower().find('стакан') != -1:
                d="cup"
from imageai.Detection import ObjectDetection
import os

# Load Yolo
execution_path = os.getcwd()
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()  # Other types are TinyYOLOv3, YOLOv3
detector.setModelPath( os.path.join(execution_path , "models/resnet50_coco_best_v2.0.1.h5"))  #yolo-tiny.h5
detector.loadModel()

detections = detector.detectObjectsFromImage(input_image="images/streetview.jpg", output_image_path="images/out.jpg")

for eachObject in detections:
    print(eachObject["name"] , " : " , eachObject["percentage_probability"] )

print('*******************')
print('*******************')
print('*******************')

print(detections)
import cv2
import numpy as np
from imageai.Detection import ObjectDetection
from PIL import Image
import os
execution_path = os.getcwd()
flag = 1
threshold = 0.5
detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath( os.path.join(execution_path , "yolo.h5"))
detector.loadModel()
custom=detector.CustomObjects(person=True)
cap = cv2.VideoCapture(0)
while(True):
	ret,frame = cap.read()
	if flag is 1 :
		img1 = Image.fromarray(frame, 'RGB')
		img1.save('image1.jpg')
		detections,extracted_objects = detector.detectCustomObjectsFromImage( custom_objects=custom,
input_image=os.path.join(execution_path , "image1.jpg"), output_image_path=os.path.join(execution_path , "image3new-custom.jpg"), minimum_percentage_probability=30,extract_detected_objects=True)
		if not detections:
			continue
		else :
			flag = 0
			img = Image.open(extracted_objects[0])
 
	frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
	print(type(frame))
	res = cv2.matchTemplate(frame,template,cv2.TM_CCOEFF)	
	min_val,max_val,min_loc,max_loc = cv2.minMaxLoc(res)
Example #9
0
from imageai.Detection import ObjectDetection

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath("resnet50_coco_best_v2.0.1.h5")
detector.loadModel(detection_speed="flash")

detections = detector.detectObjectsFromImage(input_image="image2.png", output_image_path="imagenew.jpeg")

for eachObject in detections:
    print(eachObject["name"] , " : " , eachObject["percentage_probability"] )
Example #10
0
def crop(sample=False):
    file_path = os.path.dirname(os.path.abspath(__file__)).replace('\\', '/')
    origin_images_path = file_path + "/../data/raw_data/cars_train_new"
    destination_images_path = file_path + "/../data/object_detection_data/output_images_YOLO"
    final_images_path = file_path + "/../data/object_detection_data"


    if not os.path.exists(destination_images_path):
        os.makedirs(destination_images_path)

    logging.info('Starting detecting objects')
    detector = ObjectDetection()
    detector.setModelTypeAsYOLOv3()
    detector.setModelPath(file_path + "/../data/raw_data/YOLO_weights/yolo.h5")
    detector.loadModel()
    images = os.listdir(origin_images_path)

    if sample:
        images = images[:10]

    for i in images:
        detector.detectObjectsFromImage(input_image=origin_images_path + '/' + i,
                                        output_image_path=destination_images_path + "/new_{}".format(i),
                                        extract_detected_objects=True)
    logging.info('Finished detecting objects')
    logging.info('Starting assigining objects to folder output_image_cropped')
    # Keep only the biggest cut car
    dirs = list(filter(os.path.isdir, [destination_images_path + '/' + i for i in os.listdir(destination_images_path)]))

    for directory in dirs:
        files = os.listdir(directory)
        cars_size = {}
        for file in files:
            if not (file.startswith('car') or file.startswith('truck')):
                os.unlink(directory + "/" + file)
        remaining_files = os.listdir(directory)

        for file in remaining_files:
            cars_size[file] = Image.open(directory + str("/") + file).size
        if len(cars_size) > 1:
            biggest_car = None
            dim = (0, 0)

            for car in cars_size.keys():
                if cars_size[car][0] * cars_size[car][1] > dim[0] * dim[1]:
                    biggest_car = car
                    dim = cars_size[car]

            to_delete = (list(set(cars_size.keys())))
            to_delete.remove(biggest_car)

            for small_car in to_delete:
                os.unlink(directory + str("/") + small_car)

    # Rename the images as number.jpg
    dirs = filter(os.path.isdir, [destination_images_path + '/' + i for i in os.listdir(destination_images_path)])

    for directory in dirs:
        files = os.listdir(directory)
        for file in files:
            number = re.search(r"[0-9]+", str(directory))
            new_name = str(number.group()) + ".jpg"
            start = directory + str("/") + file
            end = directory + str("/") + new_name
            os.rename(start, end)

    # Put the all the cut cars into a folder named "output_images_cropped"
    dirs = filter(os.path.isdir, [destination_images_path + '/' + i for i in os.listdir(destination_images_path)])
    if not os.path.exists(final_images_path + "/output_images_cropped"):
        os.mkdir(final_images_path + "/output_images_cropped")

    for directory in dirs:
        files = os.listdir(directory)
        for file in files:
            start = directory + str("/") + file
            destination = str(final_images_path + "/output_images_cropped") + str("/") + file
            shutil.copyfile(start, destination)

    logging.info('Finished entire process')
Example #11
0
from flask import Flask, render_template, redirect, request, session, make_response
import requests
import os
import time
import json
from PIL import Image, ImageDraw, ImageFont

headers = {
    'content-type': 'application/json',
    'nep-organization': 'fba3b52c94954a4695c821eeaf6d8f66'
}

#tf
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
from imageai.Detection import ObjectDetection
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath("resnet50_coco_best_v2.1.0.h5")
detector.loadModel()

app = Flask(__name__)


@app.route("/")
def index():
    return render_template('index.html')


@app.route("/checkout")
def checkout():
    return render_template('checkout.html')
Example #12
0
from imageai.Detection import ObjectDetection
import os

execution_path = os.getcwd()

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath(
    os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel()

for filename in os.listdir("Real-World-Masked-Face-Dataset/RWMFD_part_1/0001"):
    if filename.endswith("jpg"):
        detections = detector.detectObjectsFromImage(
            input_image=os.path.join(
                "Real-World-Masked-Face-Dataset/RWMFD_part_1/0001", filename),
            output_image_path=os.path.join(execution_path, filename))
        for eachObject in detections:
            print(eachObject["name"], " : ",
                  eachObject["percentage_probability"])
    else:
        continue
Example #13
0
import time
import os
import datetime
import json
import numpy as np
import cv2
import wget
import logging

logging.basicConfig()
logging.root.setLevel(logging.INFO)

tfl_api = 'https://api.tfl.gov.uk/Place/Type/JamCam'
model_url = 'https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/yolo.h5'

detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
model_path = os.getenv('model_path', 'models/yolo.h5')
while not os.path.exists(model_path):
    wget.download(
        'https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/yolo.h5',
        out=model_path)
detector.setModelPath(model_path)
detector.loadModel()
custom_objects = detector.CustomObjects(person=True)


def get_cameras(api):
    logging.info(str(datetime.datetime.now()) + 'Loading camera list')
    r = requests.get(api)
    if r.status_code != 200:
Example #14
0
from imageai.Detection import ObjectDetection
import cv2
"""Create instance of Object Detection class"""

det = ObjectDetection()
"""Setting paths for input image, output image and pretrained model weights of tiny yolo"""

model_path = "yolo-tiny.h5"
input_path = "input.jpg"
output_path = "prediction_output.jpg"
"""Setting the model to tiny yolov3 and loading the weights from the specified path"""

det.setModelTypeAsTinyYOLOv3()
det.setModelPath(model_path)
det.loadModel()
"""Detecting objects from the image and displaying the label if the prediction has minimum 0.1 probability."""

detection = det.detectObjectsFromImage(input_image=input_path,
                                       output_image_path=output_path,
                                       minimum_percentage_probability=0.1)
"""Result: Input and output image"""

i1 = cv2.imread("input.jpg")
cv2.imshow(i1)
i2 = cv2.imread("prediction_output.jpg")
cv2.imshow(i2)
"""Total objects detected"""

print("Enter object name to get the box location")
print("Available options are:")
count = 1
Example #15
0
    def __init__(self):
        # Init node image_view_detect
        rospy.init_node("img_detect_following")
        os.system("clear")
        execution_path = os.getcwd()
        detector = ObjectDetection()
        detector.setModelTypeAsYOLOv3()
        custom_objects = detector.CustomObjects(person=True)
        detector.setModelPath(os.path.join(execution_path, "yolo.h5"))
        detector.loadModel(detection_speed="fastest")

        # publish into cmd vel
        self.pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
        self.sub = rospy.Subscriber('/raspicam_node/image/compressed', CompressedImage, self.callback)
        self.vel = Twist()
        self.rate = rospy.Rate(10)

        self.img_root_size = [640, 480]
        self.frame = 1

        self.input_image_path = "./webCamImage.jpg"
        self.output_image_path = "./outputImage.jpg"
        self.min_prob = 20

        # time sleep ps
        self.rate_sleep = rospy.Rate(10)
        self.cv_bridge = cv_bridge.CvBridge()

        while not rospy.is_shutdown():
            # check, frame = vs.read()
            # img = msg.data
            # np_arr = np.fromstring(img, np.uint8)
            # img_np = cv2.imdecode(np_arr, 1)

            # cv2.imwrite(input_image_path, frame)
            cv2.imwrite(self.input_image_path, self.frame)
            detections = detector.detectCustomObjectsFromImage(custom_objects=custom_objects,
                                                                    input_image=os.path.join(execution_path,
                                                                                             self.input_image_path),
                                                                    output_image_path=os.path.join(execution_path,
                                                                                                   self.output_image_path),
                                                                    minimum_percentage_probability=self.min_prob)
            print("DA", detections)
            if len(detections) != 0:
                x1 = detections[0]["box_points"][0]
                y1 = detections[0]["box_points"][1]
                x2 = detections[0]["box_points"][2]
                y2 = detections[0]["box_points"][3]
                # print("(", x1, ", ", y1, ")")
                # print("(", x2, ", ", y2, ")")
                image_child = [x1, y1, x2, y2]
                self.listenAndMove(self.img_root_size, x1,y1,x2,y2)
                print("--------------------------------")
            else:
                print("Not found any person")
                self.stop()
                print("--------------------------------")

            img = cv2.imread(self.output_image_path)
            cv2.imshow('frame', img)
            # self.rate_sleep.sleep()
            key = cv2.waitKey(1)
            if key == ord("q"):
                self.stop()
                break
            self.rate.sleep()

        self.stop()
Example #16
0
#     name: python3
# ---

import os
if not os.path.exists('yolo.h5'):
    os.system(
        'wget https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/yolo.h5'
    )

# +
from imageai.Detection import ObjectDetection
import os

execution_path = os.getcwd()

detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath(os.path.join(execution_path, "yolo.h5"))
detector.loadModel()
detections = detector.detectObjectsFromImage(
    input_image=os.path.join(execution_path, "demo_image.jpg"),
    output_image_path=os.path.join(execution_path, "demo_image_new.jpg"),
    minimum_percentage_probability=30)

for eachObject in detections:
    print(eachObject["name"], " : ", eachObject["percentage_probability"],
          " : ", eachObject["box_points"])
    print("--------------------------------")

# -
from IPython.display import Image
Example #17
0
import os
from imageai.Detection import ObjectDetection
print('===>Iniciando.')

model_path = "./models/yolo-tiny.h5"
input_path = "./input/"
output_path = "./output/"

file_paths = [
    os.path.join(input_path, file) for file in os.listdir(input_path)
]
files = [
    file for file in file_paths
    if os.path.isfile(file) and file.lower().endswith(".jpg")
]

# Load model
detector = ObjectDetection()
detector.setModelTypeAsTinyYOLOv3()
detector.setModelPath(model_path)
detector.loadModel()
print('===> Modelo Yolo carregado')

# Object detection
for i, file in enumerate(files):
    detection = detector.detectObjectsFromImage(
        input_image=file, output_image_path=f"{output_path}image{i}.jpg")
    print(f"\n\n=====> ITEM {i} = {file} <======")
    for eachItem in detection:
        print("===> ", eachItem["name"], " : ",
              eachItem["percentage_probability"])
Example #18
0
def Make_Features(df):

    multiple_prediction = ImagePrediction()
    multiple_prediction.setModelTypeAsDenseNet()
    multiple_prediction.setModelPath(
        "../mode/trained_models/DenseNet-BC-121-32.h5")
    multiple_prediction.loadModel()
    detector = ObjectDetection()
    detector.setModelTypeAsYOLOv3()
    detector.setModelPath("../mode/trained_models/yolo.h5")
    detector.loadModel()

    df['DenseNet'] = df['img'].swifter.apply(
        lambda x: multiple_prediction.predictImage(path_dir + x,
                                                   result_count=3))
    df['Yolo'] = df['img'].swifter.apply(
        lambda x: detector.detectObjectsFromImage(
            path_dir + x,
            output_image_path='./new.jpg',
            minimum_percentage_probability=20))
    df['DenseNet_obj'] = df['DenseNet'].swifter.apply(
        lambda x: x[0][0] + ' and ' + x[0][1] + ' and ' + x[0][2])
    df['Yolo_obj'] = df['Yolo'].swifter.apply(
        lambda x: ' '.join(word for word in [l['name'] for l in x]), axis=1)
    df['Img_txt'] = df['DenseNet_obj'] + ' ' + df['Yolo_obj']
    df['palette_color'] = df['img'].swifter.apply(
        lambda x: ColorThief(path_dir + x).get_palette(color_count=5))
    df['Bluriness'] = [
        cv2.Laplacian(cv2.imread(path_dir + x, 0), cv2.CV_64F).var()
        for x in df['img']
    ]
    df['Imagedim'] = [
        cv2.imread(path_dir + x).flatten().shape[0] for x in df['img']
    ]
    df['Yolo_unique'] = df['Yolo_obj'].swifter.apply(lambda x: len(set(x)))
    df['Yolo_N_obj'] = df['Yolo_obj'].swifter.apply(lambda x: len(x))
    #print(df.describe() )
    # First cross variable between text and image :
    df['sim_txt_img_gen'] = df.swifter.apply(
        lambda x: nlp(x.text).similarity(nlp(x.DenseNet_obj)), axis=1)
    df['sim_txt_img_objs'] = df.swifter.apply(lambda x: nlp(x.text).similarity(
        nlp(' and '.join(word[0] for word in x.Yolo_obj))),
                                              axis=1)
    # extract dominant colors from image
    df['paletCol_1'] = df['palette_color'].swifter.apply(
        lambda x: (x[0][0] * 65536 + x[0][1] * 256 + x[0][2]))
    df['paletCol_2'] = df['palette_color'].swifter.apply(
        lambda x: (x[1][0] * 65536 + x[1][1] * 256 + x[1][2]))
    df['paletCol_3'] = df['palette_color'].swifter.apply(
        lambda x: (x[2][0] * 65536 + x[2][1] * 256 + x[2][2]))
    df['paletCol_4'] = df['palette_color'].swifter.apply(
        lambda x: (x[3][0] * 65536 + x[3][1] * 256 + x[3][2]))
    df['paletCol_5'] = df['palette_color'].swifter.apply(
        lambda x: (x[4][0] * 65536 + x[4][1] * 256 + x[4][2]))
    # Get Blurry status
    # Get shapes
    df['brightness'] = [
        cv2.mean(cv2.cvtColor(cv2.imread(path_dir + x), cv2.COLOR_BGR2HSV))[1]
        / 255. for x in df['img']
    ]
    df['Saturation'] = [
        cv2.mean(cv2.cvtColor(cv2.imread(path_dir + x), cv2.COLOR_BGR2HSV))[0]
        / 255. for x in df['img']
    ]
    df['ImageValue'] = [
        cv2.mean(cv2.cvtColor(cv2.imread(path_dir + x), cv2.COLOR_BGR2HSV))[2]
        / 255. for x in df['img']
    ]

    df['word_count'] = df['text'].swifter.apply(
        lambda x: len(str(x).split(" ")))
    df['char_count'] = df['text'].str.len()
    df['stp_count'] = df['text'].swifter.apply(
        lambda x: len([x for x in x.split() if x in stop]))
    df['spc_count'] = df['text'].swifter.apply(
        lambda x: len([x for x in list(x) if x in special_char]))
    df['sentiment_txt'] = df['text'].swifter.apply(lambda x: getsentiment(x))
    df['sentiment_img'] = df['DenseNet_obj'].swifter.apply(
        lambda x: getsentiment(x))
    df['prfn_ftr'] = df['text'].swifter.apply(lambda x: nlp(x)._.is_profane)
    df['Quant'] = df['text'].swifter.apply(lambda x: len([
        y for y in nlp(x).ents
        if str(y.label_) == 'MONEY' or str(y.label_) == 'DATE' or str(y.label_)
        == 'TIME' or str(y.label_) == 'PERCENT' or str(y.label_) == 'ORDINAL'
        or str(y.label_) == 'CARDINAL' or str(y.label_) == 'QUANTITY'
    ]))
    df['Ent'] = df['text'].swifter.apply(lambda x: len([
        y for y in nlp(x).ents
        if str(y.label_) == 'PERSON' or str(y.label_) == 'NORP' or str(
            y.label_) == 'ORG' or str(y.label_) == 'LOC' or str(y.label_) ==
        'GPE' or str(y.label_) == 'WORK_OF_ART' or str(y.label_) == 'EVENT'
    ]))

    df['polarity_scores'] = df['text'].swifter.apply(
        lambda x: sid.polarity_scores(x))
    df['neg_txt'] = df['polarity_scores'].swifter.apply(lambda x: x['neg'])
    df['neu_txt'] = df['polarity_scores'].swifter.apply(lambda x: x['neu'])
    df['pos_txt'] = df['polarity_scores'].swifter.apply(lambda x: x['pos'])
    df['com_txt'] = df['polarity_scores'].swifter.apply(
        lambda x: x['compound'])
    #df = df.drop(columns=['DenseNet' ,'DenseNet_obj', 'Yolo' , 'Yolo_obj' , 'palette_color', 'polarity_scores'])
    return df
### State Machine Define
RUN_STATE = 0
WAIT_STATE = 1
SET_NEW_ACTION_STATE = 2
state = RUN_STATE # 
previous_action = -1 # no action
text_show = 'no action'

# class_text = ['run','sit','stand','walk','standup']
class_text = ['a01','a02','a03','a04','a05','a06','a07','a08','a09',
               'a10','a11','a12','a13','a14','a15','a16','a17','a18']

### ImageAI
model_path = "pretrain/yolo-tiny.h5"
# model_path = "pretrain/yolo.h5"
detector = ObjectDetection()
detector.setModelTypeAsTinyYOLOv3()
# detector.setModelTypeAsYOLOv3()
detector.setModelPath(model_path)
detector.loadModel(detection_speed="flash") #"normal"(default), "fast", "faster" , "fastest" and "flash".
custom_objects = detector.CustomObjects(person=True)


### Main
cap = cv2.VideoCapture(0)

# Crop setting
width  = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))   # float
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))  # float
length_x_q = []
length_y_q = []
Example #20
0
#
#def get_photos():
#    '''Prompt the user for the folder path, and read all the files with it.'''
#    #location = input('Please indicate whether right or left you want to label (r, l): ').lower()
#    directory = filedialog.askdirectory()
#    file_list = os.listdir(directory)
#    
#    return file_list

directory = filedialog.askdirectory()
file_list = os.listdir(directory)

execution_path = os.getcwd()

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel()

#all_photos = get_photos()
for i in file_list:
    
    detections = detector.detectObjectsFromImage(input_image=os.path.join(directory , i), output_image_path=os.path.join(directory , i[:-4]+"-new.jpg"))
#    unique_groceries = list(set(detections["name"]))
#    print(unique_groceries)
    all_items = []
    for j in detections:
        all_items.append(j["name"])
    print(i, ',', list(set(all_items)))
##    for eachObject in detections:
Example #21
0
output_image_path = os.path.join(execution_path, "output_image")
print("Input images should be in :", input_image_path)
print("Output images will be saved in :", output_image_path)

#input_image_name=args.image_name #this is the only input needed from user
#input_image_name="apple_group_image.jpg"
input_image_name = args.image_name
output_image_name = input_image_name + "_extract.jpg"

print("\n***********************************************\n")
print(os.path.join(input_image_path, args.image_name))

#we are trying to extract objects and store in a new folder name same as output file
import warnings
warnings.filterwarnings("ignore")
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath(
    os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel()
custom_objects = detector.CustomObjects(apple=True)
detections, objects_path = detector.detectCustomObjectsFromImage(
    input_image=os.path.join(input_image_path, input_image_name),
    output_image_path=os.path.join(output_image_path, output_image_name),
    custom_objects=custom_objects,
    minimum_percentage_probability=int(args.percentage),
    extract_detected_objects=True)

extracted_image_folder_name = output_image_name + "-objects" + "\\"
print("Extracted images are stored in :", extracted_image_folder_name)
extracted_image_path = os.path.join(output_image_path,
Example #22
0
 def isCat(self, name):
     execution_path = os.getcwd(
     )  # os.getcwd() zwraca bieżący katalog roboczy
     detector = ObjectDetection()
     if name == "YOLO":
         detector.setModelTypeAsYOLOv3()
         detector.setModelPath(os.path.join(execution_path, "yolo.h5"))
     elif name == "Retina":
         detector.setModelTypeAsRetinaNet()
         detector.setModelPath(
             os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5"))
     detector.loadModel()
     detections = detector.detectObjectsFromImage(
         input_image=os.path.join(execution_path, self.imagePathGen),
         output_image_path=os.path.join(execution_path, "new.jpg"))
     # wczytuje obrazek, dla którego chcemy wykryć znajdujące się na nim obiekty, tworzę nowy obrazek z
     # zaznaczonymi na nim wykrytymi obiektami
     for eachObject in detections:
         # wyświetlam nazwy wykrytych obiektów oraz pewność ich wystąpienia (prawdopodobieńswto)
         # print(eachObject["name"], " : ", eachObject["percentage_probability"])
         if eachObject["name"] == "cat" and eachObject[
                 "percentage_probability"] > 75:
             return 'cat'
Example #23
0
# -*- coding: utf-8 -*-
"""
Created on Tue Nov 19 23:50:48 2019

@author: Kim
"""

from imageai.Detection import ObjectDetection
import os

execution_path = os.getcwd()
# print(execution_path)

# 모델 설정하기
detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath( os.path.join(execution_path , "models/yolo.h5"))

# 모델 성능 설정하기
detector.loadModel(detection_speed="fast") #fast, faster, fastest, flash

# 모델에 이미지 입력 및 출력 설정하기
detections = detector.detectObjectsFromImage(
    input_image=os.path.join(execution_path , "images/street.jpg"), 
    output_image_path=os.path.join(execution_path , "image_out.jpg"), 
    minimum_percentage_probability=50)

# 실행결과 출력하기
for eachObject in detections:
    print(eachObject["name"] , " : ", eachObject["percentage_probability"], " : ", eachObject["box_points"] )
    print("--------------------------------")
Example #24
0
    def extractor(self, image_objects):

        #inizializzo la lista delle classi da chiamare per verificare le condizioni
        lista_classi = []
        #Estraggo la lista delle differenti tipologie di condizione
        lista_condizioni = list(self.condition_list.keys())

        if 'size' in lista_condizioni:
            '''
            Appendo a 'lista_classi' le diverse chiamate alla classe
            che gestisce la condizione sulla 'size' (dimensione del file). 
            Creo in questo modo due oggetti diversi, uno per soddisfare la
            dimensione minima e uno per la dimensione massima.
            '''
            min_value = self.condition_list['size']['min']
            max_value = self.condition_list['size']['max']
            lista_classi.append(
                SizeChecker(self.condition_list, min_value, max_value))

        if 'time' in lista_condizioni:

            #Appendo a 'lista_classi' le diverse chiamate alla classe
            #che gestisce la condizione sul 'time' (data di creazione del file).

            min_value = self.condition_list['time']['min']
            max_value = self.condition_list['time']['max']
            lista_classi.append(
                TimeChecker(self.condition_list, min_value, max_value))

        if 'wordlist' in lista_condizioni:
            '''
            Appendo a 'lista_classi' le diverse chiamate alla classe
            che gestisce la condizione sulla 'wordlist' (lista delle parole cercate).
            Creo tanti oggetti della classe 'OccurrenceChecker' quante sono le coppie parola-occorrenza cercate.
            '''
            for parola in self.condition_list['wordlist'].keys():
                lista_classi.append(
                    OccurrenceChecker(parola,
                                      self.condition_list['wordlist'][parola]))

        if 'objectlist' in lista_condizioni:

            detector = ObjectDetection()

            model_path = "./models/yolo-tiny.h5"
            detector.setModelTypeAsTinyYOLOv3()
            detector.setModelPath(model_path)
            detector.loadModel()
            #Appendo a 'lista_classi' le diverse chiamate alla classe
            #che gestisce la condizione sulla 'objectlist' (lista degli oggetti cercati).

            for obj in self.condition_list['objectlist'].keys():

                #controllo se l'oggetto è tra quelli riconoscibili dall'algoritmo

                if obj in image_objects:

                    lista_classi.append(
                        ImageChecker(self.condition_list, obj,
                                     self.condition_list['objectlist'][obj],
                                     detector))

        return lista_classi  #Restituisco la lista contenente la chiamata alle classi per ogni specifica condizione
Example #25
0
    def ProcessImageFolderToTextFile(self,
                                     imageFolderPath,
                                     savePath,
                                     isObjDetect=False):
        innertexts = []
        #read all images in image folder
        fileList = os.listdir(imageFolderPath)

        detector = ObjectDetection()
        if isObjDetect:
            #detect the object and obtain a list
            detector.setModelTypeAsRetinaNet()
            detector.setModelPath(
                os.path.join(imageFolderPath, "resnet50_coco_best_v2.0.1.h5"))
            detector.loadModel()
        i = 1

        #read image one by one and save the innertext in the list
        for file in fileList:
            print("Processing ", str(i), " image: ", file)
            i += 1
            if not (".png" in file.lower() or ".jpg" in file.lower()
                    or ".gif" in file.lower()):
                print("Image type wrong:", file)
            else:
                fullfilePath = os.path.abspath(
                    imageFolderPath.split("\\")[-2] + "\\" + file)
                imagebase64 = base64.b64encode(open(fullfilePath, 'rb').read())

                #Read the image - inner text and object detection
                try:
                    #read inner text
                    innerTag = (self.readImageBase64Text(
                        imagebase64,
                        "eng").replace('\n', '').replace(' ',
                                                         '').replace('|', ''))
                    #read object detection
                    if isObjDetect:
                        innerTag += (self.readImageBase64Object(
                            imagebase64,
                            detector).replace('\n',
                                              '').replace(' ',
                                                          '').replace('|', ''))
                except Exception as e:
                    print("ProcessImageFolderToTextFile ex " + fullfilePath +
                          ": " + str(e))
                #if no innertext found, write no tag
                innerTag = ("NoTag") if innerTag == "" else (innerTag.lower())

                #image in base64 formate
                imgType = imghdr.what(fullfilePath)
                imagebase64 = base64.b64encode(open(fullfilePath, 'rb').read())

                #add to list
                innertext = file + "|" + innerTag + "|data:image/" + imgType + ";base64," + str(
                    imagebase64, 'utf-8')
                innertexts.append(innertext)

        #write the information into the target file
        with open(savePath +
                  datetime.datetime.now().strftime("%m%d%Y%H%M%S%f") + ".txt",
                  'a',
                  encoding='utf-8') as the_file:
            the_file.writelines("Image|Tag|Base64\n")
            for txt in innertexts:
                the_file.writelines(txt.strip() + '\n')
Example #26
0
import cv2
import numpy as np


from controller import saveImage, randomName
import json
import os
import requests



app   = Flask(__name__)
CORS(app, resources={ r"/*": { "origins" : "*" } })

# Inicia a Lib de Detecção de Imagens
DETECTOR = ObjectDetection()
DETECTOR.setModelTypeAsYOLOv3()
DETECTOR.setModelPath( "../yolo.h5" )
DETECTOR.loadModel(detection_speed="flash")


@app.route( '/v1/detection', methods = ['POST'] )
def v1_detection():

    url_image = request.json.get('url')

    if not url_image:
        json_resp = {
            'ok' : False,
            'msg' : 'Não encontrado url',
            'data' : None
Example #27
0
            cv2.destroyAllWindows()
            stream_process.terminate()
            break
        #time.sleep(.5)


if __name__ == '__main__':
    freeze_support()  #Need this to be able to use MP in a standalone package
    print('Enter login information for the email to use for sending alerts...')
    username = input('Email address for sending email: ')
    password = input('Password:'******'Setting up detector')
    # Configure the detector based on the settings in config.ini
    if DETECTOR_MODEL == 'yolo':
        print('YOLO chosen')
        detector.setModelTypeAsYOLOv3()
        detector.setModelPath(os.path.join('models', 'yolo.h5'))
    else:
        print('Tiny-YOLO chosen')
        detector.setModelTypeAsTinyYOLOv3()
        detector.setModelPath(os.path.join('models', 'yolo-tiny.h5'))

    print('Loading model')
    detector.loadModel(detection_speed=DETECTION_SPEED)
    print('Creating BG subtractor')
    bg_subtractor = cv2.createBackgroundSubtractorMOG2(
Example #28
0
class Camera(object):
    
    detector = ObjectDetection()
    custom_objects = detector.CustomObjects(car=True, person=True, bus=True,
                                            chair=True, truck=True, 
                                            refrigerator=True, oven=True, 
                                            bicycle=True, skateboard=True, 
                                            train=True, bench=True, 
                                            motorcycle=True, bed=True, 
                                            suitcase=True) 


    def __init__(self, index=0):
        self.cap = cv2.VideoCapture(index)
        self.openni = index in (cv2.CAP_OPENNI, cv2.CAP_OPENNI2)
        self.fps = 0
        execution_path = os.getcwd()
        self.detector.setModelTypeAsYOLOv3()
        self.detector.setModelPath(os.path.join(execution_path , "yolo.h5"))
        self.detector.loadModel(detection_speed="fastest")
        print ("Model Loaded")


    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.release()

    def release(self):
        if not self.cap: return
        self.cap.release()
        self.cap = None

    def capture(self, callback, gray=False):
        if not self.cap:
            sys.exit('The capture is not ready')

        while True:

            t = cv2.getTickCount()
            
            
            if self.openni:
                if not self.cap.grab():
                    sys.exit('Grabs the next frame failed')
                ret, depth = self.cap.retrieve(cv2.CAP_OPENNI_DEPTH_MAP)
                
                # ret, frame = self.cap.retrieve(cv2.CAP_OPENNI_GRAY_IMAGE
                
                ret, frame = self.cap.retrieve(cv2.CAP_OPENNI_DEPTH_MAP
                    if gray else cv2.CAP_OPENNI_BGR_IMAGE)
                    
                detections = self.detector.detectCustomObjectsFromImage(
                        custom_objects=self.custom_objects, input_type="array",
                        input_image=frame, output_type="array",
                        minimum_percentage_probability=20)[0]
                
                if callback:
                    callback(detections, depth, self.fps)
            else:
                ret, frame = self.cap.read()
                
                detections = self.detector.detectCustomObjectsFromImage(
                        custom_objects=self.custom_objects, input_type="array",
                        input_image=frame, output_type="array",
                        minimum_percentage_probability=20)[0]
                
                if not ret:
                    sys.exit('Reads the next frame failed')
                if gray:
                    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
                if callback:
                    callback(detections, self.fps)

            t = cv2.getTickCount() - t
            self.fps = cv2.getTickFrequency() / t
            
            # esc, q
            ch = cv2.waitKey(10) & 0xFF
            if ch == 27 or ch == ord('q'):
                break



    def fps(self):
        return self.fps

    def get(self, prop_id):
        return self.cap.get(prop_id)

    def set(self, prop_id, value):
        self.cap.set(prop_id, value)
Example #29
0
from imageai.Detection import ObjectDetection
import os
from os import listdir
from os.path import isfile, join
from PIL import Image

inpath = "verify"
outpath = "verify/out"
images = [f for f in listdir(inpath) if isfile(join(inpath, f))]

execution_path = os.getcwd()

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()

# load coco dataset weights
detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5")) 
detector.loadModel()

# restrict labels in coco to only fruits
custom_objects = detector.CustomObjects(apple=True, banana=True, orange=True) 

# process images in inpath to outpath
for i in images:
    print("Doing: "+i)
    idir = inpath + "/" + i
    odir = outpath + "/" + i
    detections = detector.detectCustomObjectsFromImage(custom_objects=custom_objects, input_image=os.path.join(execution_path , idir), output_image_path=os.path.join(execution_path , odir))
    
    for eachObject in detections:
        print(eachObject["name"] , " : " , eachObject["percentage_probability"] )
Example #30
0
from imageai.Detection import ObjectDetection

detector = ObjectDetection()

model_path = "./models/yolo-tiny.h5"
input_path = "./input/1.jpg"
output_path = "./output/newimage.jpg"

detector.setModelTypeAsTinyYOLOv3()
detector.setModelPath(model_path)
detector.loadModel()
detection = detector.detectObjectsFromImage(input_image=input_path,
                                            output_image_path=output_path)

for eachItem in detection:
    print(eachItem["name"], " : ", eachItem["percentage_probability"])
Example #31
0
from imageai.Detection import ObjectDetection
import os
from time import time

execution_path = os.getcwd()

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel(detection_speed="flash")

our_time = time()
detections = detector.detectObjectsFromImage(input_image=os.path.join(execution_path , "6.jpg"), output_image_path=os.path.join(execution_path , "6flash.jpg"), minimum_percentage_probability=30)
print("IT TOOK : ", time() - our_time)
for eachObject in detections:
    print(eachObject["name"] + " : " + eachObject["percentage_probability"] )
    print("--------------------------------")