class PriusPredictor(object):
	def __init__(self, image_path, model_path, output_path):
		self.avgColor = []
		self.pcaColors = []

		self.detector = ObjectDetection()
		self.detector.setModelTypeAsYOLOv3()
		self.detector.setModelPath(model_path + "yolo.h5")
		self.detector.loadModel(detection_speed="flash")

		self.prediction = CustomImagePrediction()
		self.prediction.setModelTypeAsResNet()
		#self.prediction.setModelPath(model_path + "model_ex-012_acc-0.988819.h5")
		self.prediction.setModelPath(model_path + "model_ex-043_acc-0.996787.h5")
		self.prediction.setJsonPath(model_path + "model_class.json")
		self.prediction.loadModel(num_objects=2)

		now = time.localtime()
		self.frame_folder = str(now.tm_year) + str(now.tm_mon) + str(now.tm_mday)
		self.image_path = image_path
		self.output_path = output_path + "detection/" + self.frame_folder + "/"

		if os.path.exists(image_path) is False:
			os.mkdir(image_path)

		if os.path.exists(output_path) is False:
			os.mkdir(output_path)

		if os.path.exists(os.path.join(output_path, 'detection')) is False:
			os.mkdir(os.path.join(output_path, 'detection'))

		if os.path.exists(os.path.join(output_path, 'processed')) is False:
			os.mkdir(os.path.join(output_path, 'processed'))

		self.create_output_folder()

	def create_output_folder(self):
		if os.path.exists(self.output_path) is False:
			os.mkdir(self.output_path)

	def predict_vehicle_method(self, prediction_meta):
		detected_img = os.path.join(prediction_meta['image_path'], prediction_meta['image_name'])
		if os.path.exists(detected_img) is not True:
			detected_img = prediction_meta['image_path']

		return self.prediction.predictImage(detected_img, result_count=2)

	def predict_vehicle(self, prediction_meta):
		detected_img = prediction_meta['image_path']

		return self.prediction.predictImage(detected_img, result_count=2)

	def detect_pca(self, image):
		priusImage = PriusImage.from_path(image)
		return priusImage.has_pca_match()

	def detect_vehicle(self, meta_data):
		try:

			image = os.path.join(meta_data["image_path"], meta_data['image_name'])
			output_image = self.output_path + meta_data['image_name']
			#print("Detecting vehicle for " + meta_data['image_name'] + " -> " + output_image)
			if os.path.exists(image) is not True:
				print("File doesnt exist. File: "  + image)
			custom_objects = self.detector.CustomObjects(car=True)
			detections, objects_path = self.detector.detectCustomObjectsFromImage(custom_objects=custom_objects,
				                                                                 input_image=image,
				                                                                 extract_detected_objects=True,
				                                                                 output_image_path=output_image,
				                                                                 minimum_percentage_probability=50)

			return zip(detections, objects_path)
		except Exception as e:
			print("While detecting vehicle: " + str(e))

	def detect_vehicle_from_array(self, decoded):

			#print("Detecting vehicle for " + meta_data['image_name'] + " -> " + output_image)

		custom_objects = self.detector.CustomObjects(car=True)
		result = self.detector.detectCustomObjectsFromImage(custom_objects=custom_objects,
		                                                                     input_type="array",
			                                                                 input_image=np.array(decoded),
			                                                                 #output_type="array",
			                                                                 minimum_percentage_probability=50)

		print("Detected: " + str(result))
		return result
Esempio n. 2
0
    textSizeWidth, textSizeHeight = textSize

    ptLowerLeftTextOriginX = int(ptCenterOfTextAreaX - (textSizeWidth / 2))
    ptLowerLeftTextOriginY = int(ptCenterOfTextAreaY + (textSizeHeight / 2))
    cv2.putText(imgOriginalScene, licPlate.strChars,
                (ptLowerLeftTextOriginX, ptLowerLeftTextOriginY), intFontFace,
                fltFontScale, SCALAR_YELLOW, intFontThickness)


execution_path = os.getcwd()

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath(
    os.path.join(execution_path, "resnet50_coco_best_v2.0.1.h5"))
detector.loadModel()
# cv2.imwrite("imgg.jpg", frames)
# cv2.imwrite("imgg.jpg",frames)
detections = detector.detectObjectsFromImage(
    input_image=os.path.join(execution_path, "shah.jpg"),
    output_image_path=os.path.join(execution_path, "imagenew.jpg"))
for eachObject in detections:
    if eachObject["name"] == "car":
        # cv2.imshow("Image",eachObject["box_image"])
        number_plate(eachObject["box_image"])
        # print(eachObject["color"])
        # save_image = Image.fromarray(eachObject["box_image"],'RGB')
        # save_image.save('my.png')
        # break
        # cv2.imwrite()
        #     save_image = misc.imread(eachObject["box_image"])
class LiveDetector:
    def __init__(self):
        # Instantiate detector
        self.detector = ObjectDetection()

        # Set and load model
        self.model_path = "D:/Final-Year-Project/Object-tracking/models/yolo.h5"
        self.detector.setModelTypeAsYOLOv3()
        self.detector.setModelPath(self.model_path)
        self.detector.loadModel()

        # Set custom objects
        self.custom_objects = self.detector.CustomObjects(car=True,
                                                          motorcycle=True,
                                                          person=True,
                                                          bicycle=True,
                                                          dog=True)
        self.tracker = CentroidTracker()

    def track_objects(self, frame):
        rects = []
        names = []
        data = {}
        frame = self.pixelate_frontyard((100, 100), frame)

        returned_image, detection = self.detector.detectCustomObjectsFromImage(
            custom_objects=self.custom_objects,
            input_image=frame,
            output_type="array",
            input_type="array")
        for eachObject in detection:
            rects.append(eachObject["box_points"])
            names.append(eachObject["name"])

            (startX, startY, endX, endY) = eachObject["box_points"]
            cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 255, 0),
                          2)
            self.blur_object((startX, startY), (endX, endY), (11, 11), frame)

        objects = self.tracker.update(rects, names)

        if objects is not None:
            for objectID, objectDetails in objects.items():
                # draw both the ID of the object and the centroid of the
                # object on the output frame
                centroid = objectDetails[0]
                name = objectDetails[1]
                if self.tracker.disappeared[objectID] < 1:
                    text = name + " " + str(objectID)
                    cv2.putText(frame, text,
                                (centroid[0] - 30, centroid[1] - 10),
                                cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
                    cv2.circle(frame, (centroid[0], centroid[1]), 4,
                               (0, 255, 0), -1)

                    data[name] = objectID

        return frame, data

    def blur_object(self, topLeft, bottomRight, kSize, frame):
        x, y = topLeft[0], topLeft[1]
        w, h = bottomRight[0] - topLeft[0], bottomRight[1] - topLeft[1]

        ROI = frame[y:y + h, x:x + w]
        blur = cv2.GaussianBlur(ROI, kSize, 0)

        frame[y:y + h, x:x + w] = blur

    def blur_frontyard(self, kSize, frame):
        height, width, channel = frame.shape
        ROI_corners = np.array([[(320, 490), (895, 320), (895, height),
                                 (320, height)]],
                               dtype=np.int32)
        blurred_frame = cv2.GaussianBlur(frame, kSize, 0)
        mask = np.zeros(frame.shape, dtype=np.uint8)
        ignore_mask_color = (255, ) * channel
        cv2.fillPoly(mask, ROI_corners, ignore_mask_color)
        mask_inverse = np.ones(mask.shape).astype(np.uint8) * 255 - mask
        frame = cv2.bitwise_and(blurred_frame, mask) + cv2.bitwise_and(
            frame, mask_inverse)

        return frame

    def pixelate_frontyard(self, kSize, frame):
        height, width, channel = frame.shape
        w, h = kSize
        ROI_corners = np.array([[(320, 490), (895, 320), (895, height),
                                 (320, height)]],
                               dtype=np.int32)
        temp = cv2.resize(frame, (w, h), interpolation=cv2.INTER_LINEAR)
        pixelated_frame = cv2.resize(temp, (width, height),
                                     interpolation=cv2.INTER_NEAREST)
        mask = np.zeros(frame.shape, dtype=np.uint8)
        ignore_mask_color = (255, ) * channel
        cv2.fillPoly(mask, ROI_corners, ignore_mask_color)
        mask_inverse = np.ones(mask.shape).astype(np.uint8) * 255 - mask
        frame = cv2.bitwise_and(pixelated_frame, mask) + cv2.bitwise_and(
            frame, mask_inverse)

        return frame
Esempio n. 4
0
import os
import cv2 
from PIL import Image

cap = cv2.VideoCapture(0) 

# Load Yolo
execution_path = os.getcwd()
detector = ObjectDetection()

#detector.setModelTypeAsTinyYOLOv3()  # YOLOv3
#detector.setModelPath( os.path.join(execution_path , "models/yolo-tiny.h5"))
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() #detection_speed="fastest"

process_this_frame = True

while 1: 
    
	# reads frames from a camera 
    ret, img = cap.read() 
    # img_str = cv2.imencode('.jpg', img)[1].tostring()
    
    if process_this_frame:
		
        img = cv2.resize(img, (0,0), fx=0.5, fy=0.5)
        
        detections = detector.detectObjectsFromImage(input_type="array", input_image=img, output_type="file")
        print(detections)
Esempio n. 5
0

def save_result(result):
	with open("prius_results.json") as json_file:
		data = json.load(json_file)
		temp = data

		temp.append(result)
	write_json(data)



detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("./yolo.h5")
detector.loadModel(detection_speed="fastest")

prediction = CustomImagePrediction()
prediction.setModelTypeAsResNet()
# self.prediction.setModelPath(model_path + "model_ex-012_acc-0.988819.h5")
prediction.setModelPath("./model_ex-043_acc-0.996787.h5")
prediction.setJsonPath("./model_class.json")
prediction.loadModel(prediction_speed="fastest")

custom_objects = detector.CustomObjects(car=True)

accuracy = 0
imgs = []

data = requests.get("http://seattle.gov/trafficcams/images/15_NW_65_NS.jpg").content
decoded = cv2.imdecode(np.frombuffer(data, np.uint8), -1)
# class_text = ['run','sit','stand','walk','standup']
class_text = [
    'a01', 'a02', 'Two Hand Wave', 'a04', 'a05', 'a06', 'a07', 'a08', 'a09',
    'a10', 'a11', 'a12', 'hand clap', '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="normal"
)  #"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 = [[] for i in range(max_person)]
length_y_q = [[] for i in range(max_person)]
max_length = 5
mid_x_q = [[] for i in range(max_person)]
mid_y_q = [[] for i in range(max_person)]
max_mid = 3
Esempio n. 7
0
 def object(self, selection):
     if selection == []:
         layout = BoxLayout(orientation="vertical", spacing=10, padding=10)
         popupLabel = Label(text="No file is selected .")
         print("No file is selected .")
         closeButton = Button(background_normal='img\close.png', background_down='img\close.png',
                              size_hint=(.38, .6), pos_hint={"center_x": .5})
         layout.add_widget(popupLabel)
         layout.add_widget(closeButton)
         popup = Popup(title='Error', content=layout, size_hint=(.8, .6))
         popup.open()
         closeButton.bind(on_press=popup.dismiss)
         engine = pyttsx3.init()
         print("No file is selected .")
         if self.soundcount == 0 :
             engine.say("No file is selected .")
         engine.runAndWait()
     else :
         engine = pyttsx3.init()
         print("Please Wait .")
         if self.soundcount == 0:
             engine.say("Please Wait .")
         engine.runAndWait()
         detector = ObjectDetection()
         model_path = "./model/yolo-tiny.h5"
         input_path = selection[0]
         output_path = "{} obj.png".format(selection[0])
         detector.setModelTypeAsTinyYOLOv3()
         detector.setModelPath(model_path)
         detector.loadModel()
         detection = detector.detectObjectsFromImage(input_image=input_path, output_image_path=output_path)
         engine = pyttsx3.init()
         print("Input Path : {}".format(input_path))
         print("Output Path : {}".format(output_path))
         if self.soundcount == 0:
             engine.say("There are ")
         print("There are ")
         flag = 0
         temp=""
         items=""
         for eachItem in detection:
             print(eachItem["name"], " : ", eachItem["percentage_probability"])
             if self.soundcount == 0:
                 if eachItem["name"] != temp:
                     engine.say(eachItem["name"])
                     temp = eachItem["name"]
                     items = items + temp + ", "
             flag = flag + 1
         if flag == 0:
             if self.soundcount == 0:
                 engine.say("no object's in the image .")
             print("no object's in the image .")
         else:
             if self.soundcount == 0:
                 engine.say("in the image .")
             print("in the image .")
         layout = BoxLayout(orientation="vertical", spacing=10, padding=10)
         popupImage = Image(source = output_path)
         closeButton = Button(background_normal='img\close.png', background_down='img\close.png', size_hint=(.38, .31),
                              pos_hint={"center_x": .5})
         layout.add_widget(popupImage)
         layout.add_widget(closeButton)
         popup = Popup(title=items, content=layout, size_hint=(.8, .9))
         popup.open()
         closeButton.bind(on_press=popup.dismiss)
         engine.runAndWait()
def object_detection():
    execution_path = os.getcwd()

    detector = ObjectDetection()
    detector.setModelTypeAsRetinaNet()
    detector.setModelPath(settings.OBJECT_RECOGNITION_MODEL)
    detector.loadModel()

    done = 0
    total_photos = Photo.objects.count()
    t = datetime.now()

    for photo in Photo.objects.all().order_by('pk'):
        if not photo.exists(check_input=True):
            logger.warning("Path does not exist!", id=photo.id, name=photo.name)
            total_photos -= 1
            continue

        if photo.get_objects():
            total_photos -= 1
            continue

        ext = photo.name.split('.')[1]
        tmp_file = os.path.join(execution_path, f"photo.{ext}")
        new_tmp_file = os.path.join(execution_path, f"photo_out.{ext}")
        if os.path.islink(tmp_file):
            os.unlink(tmp_file)
        os.symlink(photo.get_input_path(), tmp_file,)

        detections = detector.detectObjectsFromImage(
            input_image=tmp_file,
            output_image_path=new_tmp_file
        )

        with transaction.atomic():
            for detection in detections:
                object_category, _ = ObjectCategory.objects.get_or_create(name=detection["name"])
                ObjectIdentification.objects.create(
                    photo=photo,
                    category=object_category,
                    confidence=detection["percentage_probability"]
                )

        os.unlink(tmp_file)
        if os.path.isfile(photo.get_output_path()):
            os.remove(photo.get_output_path())
        os.rename(new_tmp_file, photo.get_output_path())

        done += 1

        elapsed_time = (datetime.now() - t).seconds
        estimated_time_left = elapsed_time / done * (total_photos - done)
        stats = dict(
            name=photo.name,
            id=photo.pk,
            done=done,
            left=total_photos-done,
            proc=f'{"%.2f" % (done / total_photos * 100)}',
            est_left=f'{"%.2f" % estimated_time_left}s'
        )
        if not len(detections):
            logger.warning(
                "No objects found!",
                **stats
            )
        else:
            logger.info(
                "Detecting",
                **stats
            )

    logger.info("Finished image recognition")
def main(origin_images_path="../data/raw_data/cars_train",
         destination_images_path="../data/object_detection_data/output_images_YOLO",
         final_images_path="../data/object_detection_data", sample=True):

    file_path = (os.path.dirname(os.path.abspath(__file__))).replace('\\', '/')
    if not os.path.exists(destination_images_path):
        os.makedirs(destination_images_path)

    logging.info('Starting detecting objects')
    detector = ObjectDetection()
    detector.setModelTypeAsYOLOv3()
    detector.setModelPath("../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')
Esempio n. 10
0
class ObjectOnlyCompressor:
    def __init__(self, model_path, api_key):
        self.detector = ObjectDetection()
        self.detector.setModelTypeAsRetinaNet()
        self.detector.setModelPath(model_path)
        self.detector.loadModel()
        self.api_key = api_key

    def compress(self, input_path, output_path):
        image = Image.open(input_path).convert('RGB')
        image.thumbnail((255, 255))
        items = self.detector.detectObjectsFromImage(
            input_image=np.asarray(image),
            input_type='array',
            output_image_path='debug.jpg',
        )

        file_content = bytearray()
        for item in items:
            file_content.extend(item['name'].encode('ascii'))
            file_content.append(0)
            file_content.extend(item['box_points'])

        with open(output_path, 'wb') as file:
            file.write(file_content)

    def decompress(self, input_path, output_path):
        with open(input_path, 'rb') as input_file:
            input_file_contents = input_file.read()

        output_image = Image.new('RGB', (255, 255), (255, 255, 255))

        items = []
        while input_file_contents:
            delimiter_index = input_file_contents.index(0)

            name = input_file_contents[:delimiter_index].decode('ascii')
            box_points = [
                int(input_file_contents[delimiter_index + offset])
                for offset in range(1, 5)
            ]

            items.append((name, box_points))

            input_file_contents = input_file_contents[delimiter_index + 5:]

        for item in items:
            name, box_points = item

            print(name, box_points)

            response = requests.post("https://api.deepai.org/api/text2img",
                                     data={'text': name},
                                     headers={'api-key': self.api_key})
            url = response.json()['output_url']

            image = fetch_image(url)
            image = image.resize((
                box_points[2] - box_points[0],
                box_points[3] - box_points[1],
            ))

            output_image.paste(image, (box_points[0], box_points[1]))

        output_image.save(output_path)
Esempio n. 11
0
class App:
    def __init__(self, master, portV):
        # initiating Arduino serial port
        if portV:
            try:
                self.arduino = serial.Serial(portV, 9600)
            except (serial.SerialException):
                tk.messagebox.showinfo("Error", "Invalid port.")
        else:
            self.arduino = None

        self.cam = cv2.VideoCapture(1)
        self.lightMode = 0
        self.followMode = True
        self.height = 0
        self.width = 0
        self.xBound = 0
        self.yBound = 0
        self.x = 0
        self.y = 0

        # Initiating object detection model
        self.detector = ObjectDetection()
        self.detector.setModelTypeAsYOLOv3()
        self.detector.setModelPath("yolo.h5")
        self.custom = self.detector.CustomObjects(person=True)
        self.detector.loadModel("faster")

        self.start = 0

        # Creating GUI
        self.master = master
        master.title('Feed')
        self.cFont = font.Font(family='Comic Sans MS',
                               size=15,
                               weight=font.BOLD)
        frame = tk.Frame(master, bg='white')
        frame.pack()

        self.timeLabel = tk.Label(frame,
                                  text="FPS: ",
                                  fg='white',
                                  bg='orange',
                                  width=15,
                                  height=1)
        self.timeLabel['font'] = self.cFont
        if showFeed:
            self.timeLabel.grid(row=0, column=0)
        else:
            self.timeLabel.grid(row=0, column=1)

        self.lightToggle = tk.Button(frame,
                                     text="Light:\nOff",
                                     fg='white',
                                     bg='orange',
                                     height=3,
                                     width=15,
                                     cursor='arrow',
                                     command=self.toggleLight)
        self.lightToggle['font'] = self.cFont
        if showFeed:
            self.lightToggle.grid(row=2, column=0, sticky='W')
        else:
            self.lightToggle.grid(row=1, column=0)

        self.followToggle = tk.Button(frame,
                                      text="Following:\nTrue",
                                      fg='white',
                                      bg='orange',
                                      height=3,
                                      width=15,
                                      cursor='arrow',
                                      command=self.toggleFollow)
        self.followToggle['font'] = self.cFont
        if showFeed:
            self.followToggle.grid(row=2, column=0)
        else:
            self.followToggle.grid(row=1, column=1)

        self.stop = tk.Button(master=frame,
                              text="Stop",
                              fg='white',
                              bg='orange',
                              height=3,
                              width=15,
                              cursor='arrow',
                              command=self.master.destroy)
        self.stop['font'] = self.cFont
        if showFeed:
            self.stop.grid(row=2, column=0, sticky='E')
        else:
            self.stop.grid(row=1, column=2)

        ret, output = self.cam.read()

        # Set image bounds
        self.height, self.width, channels = output.shape
        self.xBound = self.width / 10
        self.yBound = self.height / 8

        detPic, det = self.detector.detectCustomObjectsFromImage(
            custom_objects=self.custom,
            input_type="array",
            input_image=output,
            output_type="array",
            minimum_percentage_probability=15)

        if showFeed:
            imageN = ImageTk.PhotoImage(image=Image.fromarray(detPic))
            self.imgLabel = tk.Label(frame, image=imageN)
            self.imgLabel.grid(row=1, column=0)

        self.loop()

    # Turn light on and off
    def toggleLight(self):
        if self.lightMode == 0:
            self.lightMode = 1
            self.lightToggle.configure(text="Light:\nOn")
        else:
            self.lightMode = 0
            self.lightToggle.configure(text="Light:\nOff")

    # Turn object detection and following on/off
    def toggleFollow(self):
        self.followMode = not self.followMode
        if self.followMode:
            self.followToggle.configure(text="Following:\nTrue")
        else:
            self.followToggle.configure(text="Following:\nFalse")

    def loop(self):
        self.start = time.time()
        ret, output = self.cam.read()

        # If follow mode is set to on
        if self.followMode:
            # Detect hands
            detPic, det = self.detector.detectCustomObjectsFromImage(
                custom_objects=self.custom,
                input_type="array",
                input_image=output,
                output_type="array",
                minimum_percentage_probability=15)

            # If hand is detected
            if len(det) > 0:
                #Get location of detected objects
                location = det[0]['box_points']
                self.x = (location[0] + location[2]) / 2
                self.y = (location[1] + location[3]) / 2
                if (self.x > (self.width / 2 + self.xBound)):
                    self.x = 2
                elif (self.x < (self.width / 2 - self.xBound)):
                    self.x = 0
                else:
                    self.x = 1

                if (self.y > (self.height / 2 + self.yBound)):
                    self.y = 0
                elif (self.y < (self.height / 2 - self.yBound)):
                    self.y = 2
                else:
                    self.y = 1
            else:
                self.x = 1
                self.y = 1

            print(str(self.x) + " " + str(self.y) + " " + str(self.lightMode))

            # Send to Arduino over serial
            if self.arduino is not None:
                self.arduino.write(
                    struct.pack('>BBB', self.x, self.y, self.lightMode))
        else:
            # If nothing is detected
            concatted = "1 1 " + str(self.lightMode)

            print(concatted)
            if self.arduino is not None:
                self.arduino.write(struct.pack('>BBB', 1, 1, self.lightMode))

        if showFeed:
            # Display image
            if self.followMode:
                imageN = ImageTk.PhotoImage(image=Image.fromarray(detPic))
            else:
                imageN = ImageTk.PhotoImage(image=Image.fromarray(output))

            self.imgLabel.configure(image=imageN)
            self.imgLabel.image = imageN

            # FPS counter
            self.timeLabel.configure(text="FPS: " +
                                     str(1 / (time.time() - self.start))[0:5])
        else:
            if self.followMode:
                self.timeLabel.configure(
                    text="FPS: " + str(1 / (time.time() - self.start))[0:5])
            else:
                self.timeLabel.configure(text="FPS: Very Fast")

        # Loop back to itself
        self.master.after(1, self.loop)
Esempio n. 12
0
def predict():

	if request.method == 'POST':
		from keras import backend as K
		K.clear_session()
		print("POST")

		# loading model here in background
		detector = ObjectDetection()

		# use fat yolo since tiny yolo sux
		detector.setModelTypeAsYOLOv3()
		path = "./models/yolo.h5"

		detector.setModelPath(path)
		detector.loadModel()

		# creating list of custom objects
		custom = detector.CustomObjects(backpack=True, umbrella=True, cup=True)

		object_plastic_map = plastic_dict.plastic_dict

		# req_data = request.get_json()
		# image = req_data["image"]

		print(list(request.form.keys()))

		str_image = request.form["image"]
		print(str_image[:20])
		str_image = base64.b64decode(str_image)
		print(str_image[:20])

		# str_image = str_image.decode('utf-8')

		# str_image = base64_bytes.decode("base64")
		# print(str_image)

		# converting bytes information into string into np array
		# str_image = image.decode("utf-8")
		# print(str_image)



		image = Image.open(BytesIO(str_image)).convert("RGB")

		# image = ast.literal_eval(str_image)
		# print(image)
		im_array = np.array(image,dtype=np.uint8)
		# print(np.shape(image))
		print(np.shape(im_array))
		detection = detector.detectCustomObjectsFromImage(custom_objects=custom,
												  input_type="array", input_image=im_array,
												  output_type="array",
												  minimum_percentage_probability=70)

		K.clear_session()

		to_return = ""
		for eachItem in detection[1]:
			name = eachItem["name"]
			print(name + " : ", eachItem["percentage_probability"])
			to_return += name
			# to_return += ":"
			# to_return += str(object_plastic_map[name])
			to_return += str(",")

		if to_return == "":
			print("nothing detected")

		print(to_return)

		return to_return
	else:
		return no_input()
# In[2]:

model_path = os.getcwd()

# In[3]:

PRE_TRAINED_MODELS = ["yolo.h5"]

# In[4]:

# create imageAI objects and load models
object_detector = ObjectDetection()
object_detector.setModelTypeAsYOLOv3()
object_detector.setModelPath(os.path.join(model_path, PRE_TRAINED_MODELS[0]))
object_detector.loadModel()
object_detections = object_detector.detectObjectsFromImage(
    input_image="people_umbrella.jpg")

# In[5]:

#define model paths and allow file extention
UPLOAD_FOLDER = model_path
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])

# In[6]:

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

# In[7]:
def main():
    # setup folders
    image_path = "../kaggle/challenge2018_test/"
    model_weight_path = "resnet50_coco_best_v2.0.1.h5"

    print('Reading and transformin class information ...')
    # Read classes from RetinaNet. Prepared list taken from: https://github.com/fizyr/keras-retinanet/blob/master/examples/ResNet50RetinaNet.ipynb
    labels_to_names = {
        0: 'person',
        1: 'bicycle',
        2: 'car',
        3: 'motorcycle',
        4: 'airplane',
        5: 'bus',
        6: 'train',
        7: 'truck',
        8: 'boat',
        9: 'traffic light',
        10: 'fire hydrant',
        11: 'stop sign',
        12: 'parking meter',
        13: 'bench',
        14: 'bird',
        15: 'cat',
        16: 'dog',
        17: 'horse',
        18: 'sheep',
        19: 'cow',
        20: 'elephant',
        21: 'bear',
        22: 'zebra',
        23: 'giraffe',
        24: 'backpack',
        25: 'umbrella',
        26: 'handbag',
        27: 'tie',
        28: 'suitcase',
        29: 'frisbee',
        30: 'skis',
        31: 'snowboard',
        32: 'sports ball',
        33: 'kite',
        34: 'baseball bat',
        35: 'baseball glove',
        36: 'skateboard',
        37: 'surfboard',
        38: 'tennis racket',
        39: 'bottle',
        40: 'wine glass',
        41: 'cup',
        42: 'fork',
        43: 'knife',
        44: 'spoon',
        45: 'bowl',
        46: 'banana',
        47: 'apple',
        48: 'sandwich',
        49: 'orange',
        50: 'broccoli',
        51: 'carrot',
        52: 'hot dog',
        53: 'pizza',
        54: 'donut',
        55: 'cake',
        56: 'chair',
        57: 'couch',
        58: 'potted plant',
        59: 'bed',
        60: 'dining table',
        61: 'toilet',
        62: 'tv',
        63: 'laptop',
        64: 'mouse',
        65: 'remote',
        66: 'keyboard',
        67: 'cell phone',
        68: 'microwave',
        69: 'oven',
        70: 'toaster',
        71: 'sink',
        72: 'refrigerator',
        73: 'book',
        74: 'clock',
        75: 'vase',
        76: 'scissors',
        77: 'teddy bear',
        78: 'hair drier',
        79: 'toothbrush'
    }
    retina_classes = pd.DataFrame(list(labels_to_names.items()),
                                  columns=['ID', 'Label'])

    # Read classes from Google
    google_classes = pd.read_csv("class-descriptions-boxable.csv",
                                 delimiter=";",
                                 names=['ID', 'Label'])
    google_classes['Label'] = google_classes['Label'].apply(str.lower)

    # Iterate over Retina_classes and check if they're in Google classes.
    # If so, take google ID in new class mappings

    label_mappings = pd.DataFrame(columns=['ID', 'Label'])
    coco_to_google = {}

    for i in range(0, retina_classes.shape[0]):
        for j in range(0, google_classes.shape[0]):
            if (retina_classes["Label"][i] == google_classes["Label"][j]):
                label_mappings.loc[i] = google_classes.iloc[j]
                coco_to_google[retina_classes["Label"]
                               [i]] = google_classes.iloc[j]['ID']

    print('Loading resnet model ...')
    model_weight_path = "resnet50_coco_best_v2.0.1.h5"

    detector = ObjectDetection()
    detector.setModelTypeAsRetinaNet()
    detector.setModelPath(model_weight_path)
    detector.loadModel()

    print('Starting object detection method ...')
    detect_objects(detector_fn=detector.detectObjectsFromImage,
                   img_path=image_path,
                   results_fname='challenge_submission.csv',
                   minimum_percentage_probability=50,
                   translation_dict=coco_to_google)
class CaptionDataset():
    def __init__(self):

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

        self.transforms = tv.transforms.Compose(
            [tv.transforms.Resize(224),
             tv.transforms.ToTensor(), normalize])

        data = t.load('caption.pth')
        self.ix2id = data['ix2id']
        img_path = 'ai_challenger_caption_train_20170902/caption_train_images_20170902/'
        imgs = [os.path.join(img_path, self.ix2id[_]) \
                     for _ in range(len(self.ix2id))]

        self.imgs = imgs

    def __getitem__(self, index):
        img = Image.open(self.imgs[index]).convert('RGB')
        img_area = img.size[0] * img.size[1]
        detections = self.detector.detectObjectsFromImage(
            input_image=self.imgs[index])
        block_num = len(detections)
        cut_imgs = []
        if block_num == 2:
            for eachObject in detections:
                cropped = img.crop(eachObject["box_points"])
                # print(eachObject["box_points"])
                m = eachObject["box_points"][2] - eachObject["box_points"][0]
                n = eachObject["box_points"][3] - eachObject["box_points"][1]
                # print('截图宽:%d,截图高:%d' % (m, n))
                jie = (m * n) / img_area * 100
                # print('截图占比:%.2f%%' % jie)
                if jie < 10:
                    cut_imgs.append(self.transforms(img))
                else:
                    cut_imgs.append(self.transforms(cropped))
        if block_num == 1:
            for eachObject in detections:
                cropped = img.crop(eachObject["box_points"])
                # print(eachObject["box_points"])
                m = eachObject["box_points"][2] - eachObject["box_points"][0]
                n = eachObject["box_points"][3] - eachObject["box_points"][1]
                # print('截图宽:%d,截图高:%d' % (m, n))
                jie = (m * n) / img_area * 100
                # print('截图占比:%.2f%%' % jie)
                if jie < 10:
                    cut_imgs.append(self.transforms(img))
                else:
                    cut_imgs.append(self.transforms(cropped))
            cut_imgs.append(self.transforms(img))
        if block_num == 0:
            for i in range(2):
                cut_imgs.append(self.transforms(img))
        if block_num > 2:
            jie_d = {}
            for eachObject in detections:
                cropped = img.crop(eachObject["box_points"])
                # print(eachObject["box_points"])
                m = eachObject["box_points"][2] - eachObject["box_points"][0]
                n = eachObject["box_points"][3] - eachObject["box_points"][1]
                # print('截图宽:%d,截图高:%d' % (m, n))
                jie = (m * n) / img_area * 100
                # print('截图占比:%.2f%%' % jie)
                jie_d[jie] = cropped
            jie_l = sorted(jie_d.keys(), reverse=True)
            for l in range(2):
                if jie_l[l] < 10:
                    cut_imgs.append(self.transforms(img))
                else:
                    cut_imgs.append(self.transforms(jie_d[jie_l[l]]))
        return cut_imgs, index

    def __len__(self):
        return len(self.imgs)
Esempio n. 16
0
sigmaM = 0.0001
sigmaZ = 3 * noise

Q = sigmaM**2 * np.eye(4)
R = sigmaZ**2 * np.eye(2)

listCenterX = []
listCenterY = []
listpuntos = []

execution_path = os.getcwd()
detector = ObjectDetection()
detector.setModelTypeAsTinyYOLOv3()
detector.setModelPath(os.path.join(execution_path, "yolo-tiny.h5"))
detector.loadModel(detection_speed="flash")
custom = detector.CustomObjects(sports_ball=True)
cap = cv2.VideoCapture(0)
x = ()
while (True):
    ret, frame = cap.read()
    img1 = Image.fromarray(frame, 'RGB')
    img1.save('image1.jpg')
    detections, extracted_objects_array = 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"),
        extract_detected_objects=True,
        minimum_percentage_probability=30)
    for detection, object_path in zip(detections, extracted_objects_array):
        print(object_path)
Esempio n. 17
0
def car_detections():
    images = {}

    with open('./detections/jpglist.json', 'r') as f:
        jpgs = f.read()

    paths = json.loads(jpgs)

    imgdet = 0  # downloads es el contador que indica en cual link arranca la descarga de imagenes

    images_number = 300000  # aca va el numero de links guardados en el json

    for imgdet in range(imgdet, images_number):
        #  en caso de no poder ver el error en consola y ver en que imagen te quedaste
        #  creo un archivo que va guardando el ultimo estado de imgdet, o sea la ultima imagen que analicé

        images['images'] = imgdet
        with open('./detections/info.json', "w") as file:
            json.dump(images, file)

        realimage = paths[
            'jpg' +
            str(imgdet
                )]  # realimage es la imagen original la cual hay que analizar

        execution_path = os.getcwd()

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

        print("Analazing", realimage, "/ image", imgdet + 1, "of",
              images_number)

        # defino de que sitio web proviene para luego colocarlo en el json
        if 'ml' in realimage:
            website = 'ml'
        elif 'olx' in realimage:
            website = 'olx'
        elif 'demotores' in realimage:
            website = 'demotores'
        elif 'rosariogarage' in realimage:
            website = 'rosariogarage'
        else:
            website = 'unknown'

        custom_objects = detector.CustomObjects(car=True)
        detections = detector.detectCustomObjectsFromImage(
            input_type="file",
            custom_objects=custom_objects,
            input_image=os.path.join(execution_path, realimage),
            output_image_path=os.path.join("./detections/images/car" +
                                           str(imgdet + 1) + ".jpg"),
            output_type="file",
            minimum_percentage_probability=90,
            extract_detected_objects=False)

        if len(detections) == 0:  # no detecto autos
            os.remove("./detections/images/car" + str(imgdet + 1) +
                      ".jpg")  # que borre la foto y continue
            print("No cars detected")
            continue

        else:  # si detecto autos

            # a continuacion el proceso para obtener el auto de mayor relevancia

            wh = []
            sub_image = []

            for y in range(0, len(detections)):
                dots = detections[y]['box_points']
                coords = [dots[2] - dots[0], dots[3] - dots[1]]
                sub_image += [[coords[0] * coords[1], dots]]
                wh += [coords[0] * coords[1]]

            maximo = max(wh)

            for x in range(0, len(sub_image)):
                if maximo == sub_image[x][0]:
                    maximo = sub_image[x][1]

            os.remove("./detections/images/car" + str(imgdet + 1) +
                      ".jpg")  # remuevo la imagen con las detecciones
            print("Image removed")
            shutil.copy(realimage, "./detections/images/car" +
                        str(imgdet + 1) + ".jpg")  # copio la imagen original
            print("Original image saved")

            # JSON:
            # traigo los datos del auto que estan en el .json en la carpeta original para tambien guardarlos en el
            # nuevo json

            realjson = os.path.dirname(os.path.abspath(
                realimage)) + "/meta.json"  # obtengo el meta.json que esta en
            # en el mismo path que la imagen que estoy analizando

            with open(realjson, 'r') as f:
                car_data = f.read()

            data = json.loads(car_data)

            # agrego un par de datos mas al .json

            data[
                'Web Site'] = website  # guardo sitio web de donde proviene la imagen
            data[
                'boxpoints'] = maximo  # guardo los 4 puntos que forman el cuadrilatero donde se encuentra
            # el auto principal de la imagen en cuestion

            # guardo el .json

            with open("./detections/images/car" + str(imgdet + 1) + ".json",
                      'w') as fp:
                json.dump(data, fp)

            print("Created .json")

    print("End")
class StartWindows(QMainWindow):
    def __init__(self, camera=None, parent=None):
        super(StartWindows, self).__init__(parent=parent)
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        #detector

        #button
        self.ui.pushButton.clicked.connect(self.start)
        self.ui.pushButton_2.clicked.connect(self.stop)

        #camera
        self.camera = cv2.VideoCapture(0)
        #timer
        self.update_timer = QTimer()
        self.update_timer.timeout.connect(self.update)

    def start(self):
        model = self.ui.comboBox.currentText()
        print(model)

        if model == "YOLO V3":
            self.yolo()
        elif model == "YOLO TINY":
            self.yolo_tiny()
        elif model == "RESNET":
            self.resnet()

    def yolo(self):
        self.update_timer.start(30)
        self.execution_path = os.getcwd()
        self.detector = ObjectDetection()
        self.detector.setModelTypeAsYOLOv3()
        self.detector.setModelPath(os.path.join(self.execution_path,
                                                "yolo.h5"))
        self.detector.loadModel(detection_speed="flash")
        print("###you are use yolo model###")

    def yolo_tiny(self):
        self.update_timer.start(30)
        self.execution_path = os.getcwd()
        self.detector = ObjectDetection()
        self.detector.setModelTypeAsTinyYOLOv3()
        self.detector.setModelPath(
            os.path.join(self.execution_path, "yolo-tiny.h5"))
        self.detector.loadModel(detection_speed="flash")
        print("###you are use yolo_tiny model###")

    def resnet(self):
        self.update_timer.start(30)
        self.execution_path = os.getcwd()
        self.detector = ObjectDetection()
        self.detector.setModelTypeAsRetinaNet()
        self.detector.setModelPath(
            os.path.join(self.execution_path, "resnet50_coco_best_v2.0.1.h5"))
        self.detector.loadModel(detection_speed="fastest")
        print("###you are use resnet model###")

    def stop(self):
        self.update_timer.stop()

    def update(self):

        ret, frame = self.camera.read()
        frame = cv2.flip(frame, 1)
        #detected
        custom = self.ui.comboBox_2.currentText()
        print(custom)
        if custom == "Person":
            custom_objects = self.detector.CustomObjects(person=True)
        elif custom == "orange":
            custom_objects = self.detector.CustomObjects(orange=True)
        elif custom == "Cell Phone":
            custom_objects = self.detector.CustomObjects(cell_phone=True)

        detected_image_array, detections = self.detector.detectCustomObjectsFromImage(
            custom_objects=custom_objects,
            input_type="array",
            input_image=frame,
            output_type="array")

        #detected_image_array, detections = self.detector.detectCustomObjectsFromImage(custom_objects=custom_objects,output_type="array",input_type="array", input_image= frame,display_percentage_probability=True, display_object_name=True)
        for eachObject in detections:
            print(eachObject["name"], " : ",
                  eachObject["percentage_probability"], " : ",
                  eachObject["box_points"])

        #resize
        detected_image_array = cv2.resize(detected_image_array, (801, 391))
        height, width, channel = detected_image_array.shape
        bytesPerLine = 3 * width

        qImg = QImage(detected_image_array.data, width, height, bytesPerLine,
                      QImage.Format_RGB888).rgbSwapped()
        pixmap01 = QPixmap.fromImage(qImg)
        pixmap_image = QPixmap(pixmap01)
        self.ui.label.setPixmap(pixmap_image)
        self.ui.label.show()
Esempio n. 19
0
from imageai.Detection import ObjectDetection
import cv2
import requests
import numpy as np
import time

detector = ObjectDetection()

detector.setModelTypeAsYOLOv3()
detector.setModelPath("yolo.h5")
detector.loadModel(detection_speed="normal")

url = ""

t = 0

tim = 0
ti = 0

while t < 10:

    #ret, img = cap.read()
    img_resp = requests.get(url)
    img_arr = np.array(bytearray(img_resp.content), dtype=np.uint8)
    img = cv2.imdecode(img_arr, -1)

    start = time.clock()
    custom = detector.CustomObjects(person=True)
    ti = ti + (time.clock() - start)
    tim = tim + ti
    ti = 0
Esempio n. 20
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("--------------------------------")
Esempio n. 21
0
from imageai.Detection import ObjectDetection
import os
import time

dir = ObjectDetection()
dir.setModelTypeAsRetinaNet()
dir.setModelPath(os.path.abspath('retinanet.h5'))
dir.loadModel(detection_speed='faster')

objects = dir.CustomObjects(car=True,
                            bus=True,
                            train=True,
                            truck=True,
                            traffic_light=True,
                            stop_sign=True)

print('----- detecting -----')
t = time.time()

det = dir.detectCustomObjectsFromImage(
    custom_objects=objects,
    input_image=os.path.abspath('imageai_test_2.jpg'),
    output_image_path=os.path.abspath('imageai_test_2_res.jpg'))

print(time.time() - t)

for each_obj in det:
    print(each_obj)
Esempio n. 22
0
'''
Editor: Ahan M R
Date: 18-06-2018
Python 3.6.0
'''
#PS1 CROWD DETECTION IN THE SITUATION
'''
From imageAI API, we import Object detection and use it to detect the object classes in the image
'''
from imageai.Detection import ObjectDetection
import os
#os.getcwd gets the current working directory of the image
execution_path = os.getcwd()
detection = ObjectDetection()
detection.setModelTypeAsRetinaNet()
detection.setModelPath( os.path.join(execution_path , "resnet50_coco_best_v2.0.1.h5"))
detection.loadModel()
detections = detection.detectObjectsFromImage(input_image=os.path.join(execution_path , "xi.jpg"), output_image_path=os.path.join(execution_path , "imagenew.jpg"))
for eachObject in detections:
    print(eachObject["name"] + " : " + eachObject["percentage_probability"])
detector.loadModel() '''

'''YOLO TINY
model_path="models/yolo-tiny.h5"
detector = ObjectDetection()
detector.setModelTypeAsTinyYOLOv3()
detector.setModelPath(model_path)
detector.loadModel() 
'''

'''RESNET'''
model_path="models/resnet50_coco.h5"
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath(model_path)
detector.loadModel(detection_speed='fast') 

video_detector = VideoObjectDetection()
video_detector.setModelTypeAsRetinaNet()
video_detector.setModelPath(model_path)
video_detector.loadModel()

objects_dict = {}

pub_str = rospy.Publisher('objects_detected', String, queue_size=100)


custom = detector.CustomObjects(bottle=True, wine_glass=True, 
cup=True, fork=True, knife=True, spoon=True, banana=True, apple=True, sandwich=True, orange=True,
 mouse=True, remote=True, cell_phone=True, book=True,  scissors=True)
Esempio n. 24
0
def main():

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

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    try:
        #s.bind((socket.gethostname(), 8000))
        s.bind((HOST, 8000))
    except socket.error as err:
        print("Error de bind")
        sys.exit()

    s.listen(5)

    img = carga_imagen("listo.png")
    canvas.create_image(0, 0, image=img, anchor=NW)

    text.config(state="normal")
    text.delete(1.0, END)
    text.insert(INSERT, "Esperando una conexion")
    text.config(state="disabled")

    while True:
        print("esperando una conexion")
        clientsocket, address = s.accept()
        print(f"Conexion desde {address} establecida ")
        f = open("recibido.png", "wb")

        img = carga_imagen("cargando1.png")
        canvas.create_image(0, 0, image=img, anchor=NW)

        full_msg = ''

        #msg = clientsocket.recv(1024)
        #print(msg)
        #temp = msg[22:]
        #full_msg += temp.decode("utf-8")
        while True:
            msg = clientsocket.recv(1024)
            #print(msg)
            if len(msg) < 1024:
                try:
                    #temp = msg[7:]
                    #full_msg += temp.decode("utf-8")
                    full_msg += msg.decode("utf-8")
                except:
                    print("No se pudo decodificar el mensaje")
                break
            try:
                full_msg += msg.decode("utf-8")
            except:
                print("No se pudo decodificar el mensaje")
        #msg = clientsocket.recv(100000)
        print(full_msg)
        try:
            js = literal_eval(str(full_msg))
        except:
            print("No se pudo convertir el mensaje a formato json")

        text.config(state="normal")
        text.delete(1.0, END)
        text.insert(
            INSERT, "Datos sensor ultrasonico: " + str(js["distancia"]) +
            "cm de distancia\n\nDatos camara: Fotografia con un peso de " +
            str(len(js["imagen"])) + " bytes\n\n")
        text.config(state="disabled")

        try:
            imagen = base64.b64decode(js["imagen"])
            f.write(imagen)
            detections = detector.detectObjectsFromImage(
                input_image=os.path.join(execution_path, "recibido.png"),
                output_image_path=os.path.join(execution_path, "imagenew.png"),
                minimum_percentage_probability=40)

            img = carga_imagen("imagenew.png")
            canvas.create_image(0, 0, image=img, anchor=NW)
            objetos_detectados = compilar1(detections)
            """if len(detections) != 0:
                for eachObject in detections:
                    objetos_detectados += ""+str(verificarGen(traducir(eachObject["name"])))+ ";" + str(eachObject["percentage_probability"] )+" "
            else:
                objetos_detectados += "una pared u objeto desconocido "
            """
            text.config(state="normal")
            text.insert(
                INSERT,
                "Objeto(s) identificado(s): " + objetos_detectados + "\n\n")

            msg_to_send = compilar2(detections, js)

            #msg_to_send = direccion(str(js["direccion"]))+"hay "+objetos_detectados+"a "+str(js["distancia"])+"cm de distancia de usted"
            print("Natural: " + msg_to_send)
            print("\nMorse: " + str(encode(msg_to_send)))

            text.insert(
                INSERT, "Mensaje en lenguaje natural: " + msg_to_send +
                "\n\nMensaje en codigo morse: " + encode(msg_to_send) + "\n\n")
            text.config(state="disabled")

            clientsocket.send(bytes(msg_to_send, "utf-8"))
            clientsocket.close()
        except:
            print("Error grave")
            clientsocket.close()
            img = carga_imagen("listo.png")
            canvas.create_image(0, 0, image=img, anchor=NW)
            text.config(state="normal")
            text.delete(1.0, END)
            text.insert(INSERT, "Esperando una conexion")
            text.config(state="disabled")
Esempio n. 25
0
#
import speech_recognition as sr
import cv2
import serial
import numpy as np
from imageai.Detection import ObjectDetection
import os
execution_path = os.getcwd()
detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()  ## NOTE: Установка модели в качестве.....
detector.setModelPath(
    os.path.join(
        execution_path,
        "C:\\Users\\Admin_Robo\\Desktop\\project\\models\\resnet50_coco_best_v2.0.1.h5"
    ))  # NOTE: Модели загрузка
detector.loadModel()  ## NOTE: + Установка скорости.
print('LOADED')
print('LOADED2')
cap = cv2.VideoCapture()
ret, frame = cap.read()
cv2.imwrite('C:\\Users\\Admin_Robo\\Desktop\\project\\images\\test.png', frame)
cap.release()
cv2.destroyAllWindows()
while True:
    ## NOTE: Обработка
    detections = detector.detectObjectsFromImage(
        input_image=os.path.join(
            execution_path,
            "C:\\Users\\Admin_Robo\\Desktop\\project\\images\\test.png"),
        output_image_path=os.path.join(
            execution_path,
Esempio n. 26
0
# ref: https://imageai.readthedocs.io/en/latest/detection/index.html

# This module will count the objects in an image and store them

import time
from imageai.Detection import ObjectDetection
import os

img_detector = ObjectDetection()
img_detector.setModelTypeAsYOLOv3()  #147 layers
img_detector.setModelPath(
    os.path.join("/catkin_ws/src/master_pkg/src/", "yolo.h5"))  #147 layers
img_detector.loadModel(
    detection_speed="fast")  #normal, fast, faster, fastest, flash
# Note increases in speed should coorespond to lower a 'min. % probability' value


def count(image_array):
    try:
        det_frame, detections = img_detector.detectObjectsFromImage(
            input_type="array",
            minimum_percentage_probability=60,
            input_image=image_array,
            output_type="array")

        print("-------------")
        for item in detections:
            print(item["name"])
        print("-------------")
    except:
        rospy.loginfo("Could not count objects.")
Esempio n. 27
0
                                                            output_file_path= os.path.join(execution_path ,"Detected {}".format(os.path.basename(file_path[:-4]))),
                                                            frames_per_second=29, minimum_percentage_probability=min_prob,  log_progress=True)
    else:
        
        with graph.as_default():
            video_path = detector.detectObjectsFromVideo(input_file_path=file_path,
                                                     output_file_path=os.path.join(execution_path ,"Detected {}".format(os.path.basename(file_path[:-4]))),
                                                     minimum_percentage_probability=min_prob, frames_per_second=29, log_progress=True)
    #video_path = detector.detectObjectsFromVideo(input_file_path=file_path,
    #                                             output_file_path=os.path.join(execution_path,"Detected {}".format(os.path.basename(file_path[:-4]))),
    #                                             frames_per_second=29, log_progress=True)
    return(video_path)
detector1 = ObjectDetection()
detector1.setModelTypeAsYOLOv3()
detector1.setModelPath("yolo.h5")
detector1.loadModel()
graph1 = tf.get_default_graph()
result=tuple()
listt=[]
prob={}
def ImageDetection(file_path,objects=None,min_prob=30):
    print(file_path)
    execution_path = r'./media/output_video'
    #detector = ObjectDetection()
    #detector.setModelTypeAsYOLOv3()
    #detector.setModelPath("yolo.h5")
    #detector.loadModel()
    if objects:
        print(objects)
        def func(person,truck,bus,bicycle,bird,motorcycle):
        #for k,v in objects.items():"%s = %s" % (k,v)
Esempio n. 28
0
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt
from imageai.Detection import ObjectDetection
import os

workdir = os.getcwd()

find_objects = ObjectDetection()
find_objects.setModelTypeAsRetinaNet()

find_objects.setModelPath(os.path.join(workdir,
                                       "resnet50_coco_best_v2.0.1.h5"))
find_objects.loadModel()

objects_found = find_objects.detectObjectsFromImage(
    input_image=os.path.join(workdir, "ilia.jpg"),
    output_image_path=os.path.join(workdir, "iliaRevealed.jpg"))

for objects in objects_found:
    print(objects["name"] + " : " + str(objects["percentage_probability"]))
def getImageDetails():
    image = request.files
    print()
    print(image)
    filestr = image['img'].read()
    #convert string data to numpy array
    npimg = numpy.fromstring(filestr, numpy.uint8)
    # convert numpy array to image
    print(1)
    img = cv2.imdecode(npimg, cv2.IMREAD_UNCHANGED)
    print(1)
    # img = cv2.imread(filename)
    cv2.imshow("image from post", img)
    cv2.imwrite("image_from_post_request.jpeg", img)
    print(1)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    # plt.imshow(img)
    # plt.show()
    filename = "image_from_post_request.jpeg"
    execution_path = os.getcwd()
    file_name = "image_from_post_request"
    detector = ObjectDetection()
    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, file_name + ".jpeg"),
        output_image_path=os.path.join(execution_path,
                                       file_name + "detected.jpeg"))
    image = cv2.imread(file_name + ".jpeg")
    copy = image.copy()
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
    # ROI_number = 0
    # return(jsonify({"out" : "Success !!!!"}))
    output = {
        "humna_face": {
            "count": 0
        },
        "animal": {
            "count": 0,
            "objects_found": dict()
        },
        "object": {
            "count": 0,
            "objects_found": dict()
        }
    }
    animal = [
        "bird", "cat", "dog", 'horse', "sheep", "cow", "elephant", "bear",
        "zebra", "giraffe"
    ]
    for eachObject in detections:
        print(output, eachObject.keys())
        print(eachObject["name"], " : ", eachObject["percentage_probability"])
        if (eachObject['name'] == 'person'):
            x, y, w, h = eachObject['box_points']
            ROI = image[y:y + h, x:x + w]
            cv2.imwrite('ROI.jpeg', ROI)
            cv2.rectangle(copy, (x, y), (x + w, y + h), (36, 255, 12), 2)
            person = cv2.imread('ROI.jpeg')
            # Convert into grayscale
            gray = cv2.cvtColor(person, cv2.COLOR_BGR2GRAY)
            # Detect faces
            faces = face_cascade.detectMultiScale(gray, 1.4, 1, minSize=(1, 1))
            if (len(faces) == 0):
                pass
            else:
                output['human_face']['count'] += 1
        elif (eachObject['name'] in animal):
            output['animal']['count'] += 1
            if (eachObject['name']
                    not in output['animal']['objects_found'].keys()):
                output['animal']['objects_found'][eachObject['name']] = 0
            output['animal']['objects_found'][eachObject['name']] += 1

        else:
            output['object']['count'] += 1
            if (eachObject['name']
                    not in output['object']['objects_found'].keys()):
                output['object']['objects_found'][eachObject['name']] = 0
            output['object']['objects_found'][eachObject['name']] += 1

    # return(send_file(filename_or_fp = file_name + "detected.jpeg", mimetype="image/gif"))
    # return(m.to_string(), {'Content-Type': m.content_type})
    return (jsonify(output))
Esempio n. 30
0
#                            , frames_per_second=29, log_progress=True)
#print(video_path)

from imageai.Detection import ObjectDetection
import os

execution_path = os.getcwd()

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath(
    os.path.join(
        execution_path,
        "C:\\Users\\Admin_Robo\\Desktop\\resnet50_coco_best_v2.0.1.h5"))
detector.loadModel(
    detection_speed='faster'
)  ## NOTE: 1 секунда, 5 объектов, самое быстрое для распознавания.
print('LOADED')
print('LOADED2')

detections = detector.detectObjectsFromImage(
    input_image=os.path.join(
        execution_path,
        "C:\\Users\\Admin_Robo\\Desktop\\WIN_20190919_18_18_19_Pro.jpg"),
    output_image_path=os.path.join(
        execution_path, "C:\\Users\\Admin_Robo\\Desktop\\iimg666.jpg"))

print(
    '=================================IMG 1=================================')
for eachObject in detections:
    print(eachObject["name"], " : ", eachObject["percentage_probability"])
Esempio n. 31
0
class disjoin_counter:
    def __init__(self, Pass, recording_path):

        #intilizing the fundamental veriable
        self.recording_path = recording_path

        self.Pass = Pass

        self.my_col = None

    def connect(self):

        self.my_col = my_collection(self.Pass)

        if self.my_col == -1:
            print("Please enter a valid access key !")
            exit()

        print(" Disjoin counter started successfully!")

    def get_detector(self):

        # starting ObjectDetection module
        self.detector = ObjectDetection()
        self.detector.setModelTypeAsRetinaNet()
        self.detector.setModelPath('Model/resnet50_coco_best_v2.1.0.h5')
        self.detector.loadModel()

    def start(self, store_output_video=False):

        self.connect()

        self.get_detector()

        # starting the video capture activity

        vid = cv2.VideoCapture('input_video.mp4')  #str(self.recording_path))

        i = 0
        j = 10

        while (vid.isOpened()):

            ret, frame = vid.read()

            if ret == True:

                if (j % 10 == 0):  # taking every 10th frame

                    t = datetime.now()
                    t = t + timedelta(seconds=1)

                    custom = self.detector.CustomObjects(person=True)

                    returned_image, detections = self.detector.detectCustomObjectsFromImage(
                        custom_objects=custom,
                        input_image=frame,
                        output_type="array",
                        minimum_percentage_probability=30,
                        input_type="array")

                    out_img = "Store/" + "imagenew" + str(i) + ".jpg"

                    if store_output_video:
                        cv2.imwrite(out_img, returned_image)

                    cv2.imshow('F', returned_image)

                    i += 1

                    count = len(detections)
                    d = t.strftime("%m/%d/%Y, %H:%M:%S")

                    mydict = {"_id": i, "count": count, "Date_time": d}

                    x = self.my_col.insert_one(
                        mydict)  # inserting into database

                    if cv2.waitKey(25) & 0xFF == ord('q'):
                        break
                j += 1
            else:

                break

        vid.release()
        #out.release()

        cv2.destroyAllWindows()