예제 #1
0
 def __init__(self):
     self.methods_map = {
         SystemWorkStates.image.value: self.work_on_image,
         SystemWorkStates.video.value: self.work_on_video,
         SystemWorkStates.webcam.value: self.work_on_webcam
     }
     self.imagecapture = ImageCapture()
예제 #2
0
    def __init__(self) -> None:

        super().__init__()
      
        self.window = QtWidgets.QMainWindow()
        self.init_ui()
        self.imageCaptureDelegate = ImageCapture() #call image capture class
예제 #3
0
def main():

    # Capture and
    image_capture = ImageCapture(dir_path=IMAGE_DIR_PATH, n_camera=2)
    image_capture.capture()

    image_path = image_capture.last_captured_path

    # Predict
    model_path = MODEL_PATH
    client = LocalPredictApiClient(model_path)
    pred_result = client.predict(image_path)

    # Save
    prediction_data = PredictionData(
        image_path=image_path,
        prediction=pred_result['prediction'],
        json_result=str(pred_result),
        capture_datetime_local=image_capture.capture_datetime_local,
        capture_datetime_utc=image_capture.capture_datetime_utc)

    prediction_ds = PredictionDataStore(DB_DIR_PATH)
    prediction_ds.save(prediction_data)
예제 #4
0
class RecognitionSystem:
    def __init__(self):
        self.methods_map = {
            SystemWorkStates.image.value: self.work_on_image,
            SystemWorkStates.video.value: self.work_on_video,
            SystemWorkStates.webcam.value: self.work_on_webcam
        }
        self.imagecapture = ImageCapture()

    def work_on_image(self):
        frame = self.imagecapture.capture_photo()
        known_face_encodings = [
            item.get_encoding() for item in self.data_students
        ]
        known_face_names = [item.get_name() for item in self.data_students]
        results = {}
        face_locations = face_recognition.face_locations(frame)
        face_encodings = face_recognition.face_encodings(frame, face_locations)

        for face_encoding, face_location in zip(face_encodings,
                                                face_locations):
            face_distances = face_recognition.face_distance(
                known_face_encodings, face_encoding)
            best_match_index = np.argmin(face_distances)
            name = known_face_names[best_match_index]
            dist = face_distances[best_match_index]
            results[name] = {'confidence': dist, 'bbox': face_location}
        return results, frame

    def work_on_webcam(self):
        video_capture = cv2.VideoCapture(0)
        known_face_encodings = [
            item.get_encoding() for item in self.data_students
        ]
        known_face_names = [item.get_name() for item in self.data_students]
        results = {}
        tmp_result = {}
        process_this_frame = True

        while True:
            # Grab a single frame of video
            ret, frame = video_capture.read()
            # Resize frame of video to 1/4 size for faster face recognition processing
            small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
            # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
            rgb_small_frame = small_frame[:, :, ::-1]

            # Only process every other frame of video to save time
            if process_this_frame:
                tmp_result.clear()
                # Find all the faces and face encodings in the current frame of video
                face_locations = face_recognition.face_locations(
                    rgb_small_frame)
                face_encodings = face_recognition.face_encodings(
                    rgb_small_frame, face_locations)
                # Сравнение каждого детектированного лица с массивом лиц базы данных

                for face_encoding, face_location in zip(
                        face_encodings, face_locations):
                    face_distances = face_recognition.face_distance(
                        known_face_encodings, face_encoding)
                    best_match_index = np.argmin(face_distances)
                    name = known_face_names[best_match_index]
                    dist = face_distances[best_match_index]
                    tmp_result[name] = {
                        'confidence': dist,
                        'bbox': face_location
                    }
            # Display the results
            IO.show_video_frame(tmp_result, frame)
            for name in tmp_result:
                if name not in results.keys():
                    results[name] = {
                        'confidence': tmp_result[name]['confidence']
                    }
                else:
                    results[name]['confidence'] = (
                        results[name]['confidence'] +
                        tmp_result[name]['confidence']) / 2
            # Hit 'q' on the keyboard to quit!
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
            process_this_frame = not process_this_frame
        # Release handle to the webcam
        video_capture.release()
        cv2.destroyAllWindows()
        return results, frame

    def work_on_video(self):
        video_fragment = self.imagecapture.capture_video()
        known_face_encodings = [
            item.get_encoding() for item in self.data_students
        ]
        known_face_names = [item.get_name() for item in self.data_students]
        video_fragment = [
            fragment for index, fragment in enumerate(video_fragment)
            if index % 4 == 0
        ]
        results = {}
        tmp_result = {}
        for frame in video_fragment:
            small_frame = ImageTransforms.image_to_small(frame)
            # Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
            rgb_small_frame = small_frame[:, :, ::-1]
            face_locations = face_recognition.face_locations(rgb_small_frame)
            face_encodings = face_recognition.face_encodings(
                rgb_small_frame, face_locations)
            for face_encoding, face_location in zip(face_encodings,
                                                    face_locations):
                face_distances = face_recognition.face_distance(
                    known_face_encodings, face_encoding)
                best_match_index = np.argmin(face_distances)
                name = known_face_names[best_match_index]
                dist = face_distances[best_match_index]
                tmp_result[name] = {'confidence': dist, 'bbox': face_location}
            for face_encoding, face_location in zip(face_encodings,
                                                    face_locations):
                face_distances = face_recognition.face_distance(
                    known_face_encodings, face_encoding)
                best_match_index = np.argmin(face_distances)
                name = known_face_names[best_match_index]
                dist = face_distances[best_match_index]
                if name not in results:
                    results[name] = {'confidence': dist}
                else:
                    results[name]['confidence'] = (
                        results[name]['confidence'] + dist) / 2
                results[name]['bbox'] = face_location
            IO.show_video_frame(tmp_result, frame)
            for name in tmp_result:
                if name not in results.keys():
                    results[name] = {
                        'confidence': tmp_result[name]['confidence']
                    }
                else:
                    results[name]['confidence'] = (
                        results[name]['confidence'] +
                        tmp_result[name]['confidence']) / 2
            # Hit 'q' on the keyboard to quit!
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
            # Release handle to the webcam
        cv2.destroyAllWindows()
        return results, video_fragment

    def run(self, mode, data_students):
        self.data_students = data_students
        work_method = self.methods_map.get(mode)
        results, frame = work_method()
        return results, frame
예제 #5
0
    newpath = cwd + "/" + datafolder
    if not os.path.exists(newpath):
        os.makedirs(newpath)
    imagefile = datafolder + filename + '.png'
    textfile = open(datafolder + filename + '.txt', 'a')

    file_operations.save_to_folder(textfile, imagefile, bounding_box, final)


# todo get classes through GUI
classes = []

#interface = CLI()
file_operations = FileOperations()
motor = MotorControl()
camera = ImageCapture(RPI_CAMERA)
image_processor = ImageProcessing(camera.capture())

delay = 1 / 1000.0
#images = input("How many images do you want per category (5 categories)?")
images = 10000
STEPS_FOR_FULL_CIRCLE = 12360
steps = int(STEPS_FOR_FULL_CIRCLE / images)
classes = ["Banana"]  #, "Rolle"]

only_snippets = False
only_train_images = True

## Section for the configuration
# Make images for every class
for label in classes:
예제 #6
0
from image_capture import ImageCapture
from image_processing import ImageProcessing
import cv2
import numpy as np

camera = ImageCapture()
#camera = cv2.VideoCapture(0)
processor = ImageProcessing()

while True:
    img_raw = camera.capture()
    img_cut = img_raw[:,
                      int(np.shape(img_raw)[1] * 1 /
                          5):int(np.shape(img_raw)[1] * 4 / 5), :]
    img_gray = processor.gray(img_cut)
    edge = processor.canny(img_gray)
    contour = processor.max_contour(edge)
    cv2.drawContours(img_cut, contour, -1, (0, 255, 0), 3)
    bounding_box = processor.bounding_box(contour)
    print(bounding_box)
    #if bounding_box != -1:
    #    print("success!")
    #else:
    #    print("failure!")
    cv2.imshow('raw_image', img_raw)
    cv2.imshow('cut_image', img_cut)
    cv2.imshow('gray_image', img_gray)
    cv2.imshow('canny_image', edge)
    cv2.waitKey(20)
예제 #7
0
    def run(self):
        """ Main Challenge method. Has to exist and is the
            start point for the threaded challenge. """
        # Startup sequence
        if self.core_module is None:
            # Initialise GPIO
            GPIO.setwarnings(False)
            GPIO.setmode(GPIO.BCM)

            # Instantiate CORE / Chassis module and store in the launcher.
            self.core_module = core.Core(GPIO)
            # Limit motor speeds in AutoMode
            self.core_module.set_speed_factor(0.6)  # 0.6 on old motors
            self.core_module.enable_motors(True)

        # wait for the user to enable motors
        while not self.core_module.motors_enabled():
            time.sleep(0.25)

        # Load our previously learned arena colour order
        self.camera = picamera.PiCamera()
        self.processor = StreamProcessor(self.core_module, self.camera)
        print('Wait ...')
        time.sleep(2)

        filename = "arenacolours.txt"
        if os.path.isfile(filename):
            with open(filename) as f:
                content = f.readlines()
            if len(content) > 0:
                self.processor.arenacolours = [x.strip() for x in content]
                self.processor.state = State.ORIENTING
            f.close()

        # Setup the camera
        frameRate = 30  # Camera image capture frame rate
        self.camera.resolution = (self.processor.imageWidth,
                                  self.processor.imageHeight)
        self.camera.framerate = frameRate
        self.camera.awb_mode = 'off'

        # Load the exposure calibration
        redgain = 1.5  # Default Gain values
        bluegain = 1.5
        filename = "rbgains.txt"
        if os.path.isfile(filename):
            with open(filename) as f:
                content = f.readlines()
            content = [x.strip() for x in content]
            redgain = float(content[0][2:])
            bluegain = float(content[1][2:])
            f.close()
        self.camera.awb_gains = (redgain, bluegain)

        self.captureThread = ImageCapture(self.camera, self.processor)

        try:
            # Loop indefinitely until we are no longer running
            while self.processor.state != State.FINISHED:
                # Wait for the interval period
                #
                time.sleep(0.1)
        except KeyboardInterrupt:
            print("User shutdown\n")
        except Exception as e:
            print(e)

        self.core_module.enable_motors(False)
        self.processor.state = State.FINISHED
        self.captureThread.join()
        self.processor.terminated = True
        self.processor.join()
        self.camera.close()  # Ensure camera is release
        # del self.camera
        self.camera = None
        print("Challenge terminated")
예제 #8
0
class Rainbow:
    def __init__(self, core_module, oled):
        """Class Constructor"""
        self.killed = False
        self.core_module = core_module
        self.ticks = 0
        self.oled = oled
        self.camera = None
        self.captureThread = None

    def show_state(self):
        """ Show motor/aux config on OLED display """
        if self.oled is not None:
            # Format the speed to 2dp
            if self.core_module and self.core_module.motors_enabled():
                message = "Rainbow: %0.2f" % (
                    self.core_module.get_speed_factor())
            else:
                message = "Rainbow: NEUTRAL"

            self.oled.cls()  # Clear Screen
            self.oled.canvas.text((10, 10), message, fill=1)
            # Now show the mesasge on the screen
            self.oled.display()

    def stop(self):
        """Simple method to stop the RC loop"""
        self.killed = True
        if self.processor:
            self.processor.state = State.FINISHED
        if self.captureThread:
            self.captureThread.stop()

    def run(self):
        """ Main Challenge method. Has to exist and is the
            start point for the threaded challenge. """
        # Startup sequence
        if self.core_module is None:
            # Initialise GPIO
            GPIO.setwarnings(False)
            GPIO.setmode(GPIO.BCM)

            # Instantiate CORE / Chassis module and store in the launcher.
            self.core_module = core.Core(GPIO)
            # Limit motor speeds in AutoMode
            self.core_module.set_speed_factor(0.6)  # 0.6 on old motors
            self.core_module.enable_motors(True)

        # wait for the user to enable motors
        while not self.core_module.motors_enabled():
            time.sleep(0.25)

        # Load our previously learned arena colour order
        self.camera = picamera.PiCamera()
        self.processor = StreamProcessor(self.core_module, self.camera)
        print('Wait ...')
        time.sleep(2)

        filename = "arenacolours.txt"
        if os.path.isfile(filename):
            with open(filename) as f:
                content = f.readlines()
            if len(content) > 0:
                self.processor.arenacolours = [x.strip() for x in content]
                self.processor.state = State.ORIENTING
            f.close()

        # Setup the camera
        frameRate = 30  # Camera image capture frame rate
        self.camera.resolution = (self.processor.imageWidth,
                                  self.processor.imageHeight)
        self.camera.framerate = frameRate
        self.camera.awb_mode = 'off'

        # Load the exposure calibration
        redgain = 1.5  # Default Gain values
        bluegain = 1.5
        filename = "rbgains.txt"
        if os.path.isfile(filename):
            with open(filename) as f:
                content = f.readlines()
            content = [x.strip() for x in content]
            redgain = float(content[0][2:])
            bluegain = float(content[1][2:])
            f.close()
        self.camera.awb_gains = (redgain, bluegain)

        self.captureThread = ImageCapture(self.camera, self.processor)

        try:
            # Loop indefinitely until we are no longer running
            while self.processor.state != State.FINISHED:
                # Wait for the interval period
                #
                time.sleep(0.1)
        except KeyboardInterrupt:
            print("User shutdown\n")
        except Exception as e:
            print(e)

        self.core_module.enable_motors(False)
        self.processor.state = State.FINISHED
        self.captureThread.join()
        self.processor.terminated = True
        self.processor.join()
        self.camera.close()  # Ensure camera is release
        # del self.camera
        self.camera = None
        print("Challenge terminated")
class TranslatorGUI(QWidget):
    """Subclass of QWidget that serves as the main window and interface for the application.

    :type self.imageCaptureDelegate: ImageCapture
    :var self.imageCaptureDelegate: Uses OpenCV to access webcam and take pictures with it.

    :type self.translateDelegate: ImageTranslator
    :var self.translateDelegate: Delegate that handles requests to Google's Cloud Vision/Translate APIs

    :type self.imageView: ImageView
    :var self.imageView: The object that displays the image and handles drawing translated words and their frames.
    """
    def __init__(self) -> None:
        super().__init__()
        self.init_ui()
        self.imageCaptureDelegate = ImageCapture()
        self.translateDelegate = ImageTranslator()

    def init_ui(self) -> None:
        """Initializes the application's Graphical User Interface.
        """

        # Create necessary layouts
        h_layout = QHBoxLayout()
        left_v_layout = QVBoxLayout()
        right_v_layout = QVBoxLayout()
        h_layout.addLayout(left_v_layout)
        h_layout.addLayout(right_v_layout)

        # Create and setup descriptive label, buttons, and combo box
        welcome_label = QLabel(
            "Welcome to the OCR (Optical Character Recognition) Translator App. "
            "Use this app to translate the text contained in images!")
        welcome_label.setWordWrap(True)
        take_pic_btn = QPushButton("Take a Picture")
        take_pic_btn.clicked[bool].connect(self.__take_picture)
        slct_img_btn = QPushButton("Select an Existing Image")
        slct_img_btn.clicked[bool].connect(self.__select_existing_image)
        translate_img_btn = QPushButton("Translate Text in Image")
        translate_img_btn.clicked[bool].connect(self.__translate_image_text)
        select_target_language_box = QComboBox()
        select_target_language_box.addItems(
            ['English', 'Spanish', 'French', 'German', 'Chinese', 'Turkish'])
        select_target_language_box.currentIndexChanged.connect(
            lambda x: self.__set_target_language(select_target_language_box))

        # Initialize ImageView instance to display image
        self.imageView = ImageView(QImage())

        # Add appropriate widgets to the left and right vertical layouts
        left_v_layout.addWidget(welcome_label)
        left_v_layout.addWidget(select_target_language_box)
        left_v_layout.addWidget(take_pic_btn)
        left_v_layout.addWidget(slct_img_btn)
        left_v_layout.addWidget(translate_img_btn)
        right_v_layout.addWidget(self.imageView)

        # setup and show window
        self.setLayout(h_layout)
        self.setWindowTitle("OCR Translator App")
        self.show()

    def __take_picture(self) -> None:
        """Launches image capture window, allows user to take image, then loads it.
        """

        image_file_name = self.imageCaptureDelegate.capture_image()
        self.__load_image(image_file_name)

    def __select_existing_image(self) -> None:
        """Launches file dialog box, allows user to select an existing image, then loads it.
        """

        file_dialog = QFileDialog()
        image_file_name = file_dialog.getOpenFileName()
        self.__load_image(image_file_name[0])

    def __set_target_language(self, box: QComboBox) -> None:
        """Sets the target language for translation requests based on the currently selected value in the combo box.
        """

        self.translateDelegate.set_target_language(box.currentText())

    def __translate_image_text(self) -> None:
        """Requests OCR translation from self.translateDelegate, and triggers drawing of translation.
        """

        data = QByteArray()
        buffer = QBuffer(data)
        self.imageView.get_current_image().save(buffer, 'JPG')

        word_boxes = self.translateDelegate.translate_image_text(data.data())
        self.imageView.draw_word_boxes(word_boxes)

    def __load_image(self, file_name: str) -> None:
        """Triggers display of image in self.imageView

        :param file_name: the file name of the image to be loaded.
        """

        self.imageView.set_image(QImage(file_name))
 def __init__(self) -> None:
     super().__init__()
     self.init_ui()
     self.imageCaptureDelegate = ImageCapture()
     self.translateDelegate = ImageTranslator()
예제 #11
0
class TranslatorGUI (QWidget):
    """Subclass of QWidget that serves as the main window and interface for the application.
    """
   
    def __init__(self) -> None:

        super().__init__()
      
        self.window = QtWidgets.QMainWindow()
        self.init_ui()
        self.imageCaptureDelegate = ImageCapture() #call image capture class

    #Initialize Window Components
    def init_ui(self) -> None:
        self.__stylingWindowOne()
        
        take_pic_btn = QtWidgets.QPushButton("Capture Image", self.window)
        take_pic_btn.clicked[bool].connect(self.__take_picture)
        take_pic_btn.setGeometry(50,370,180,40)
        take_pic_btn.setStyleSheet("background-color: #3700B3 ; font : 12px ; font-weight: bold ; color : #fff ")
        slct_img_btn = QPushButton("Select existing Image", self.window)
        slct_img_btn.clicked[bool].connect(self.__select_existing_image)
        slct_img_btn.setGeometry(50,420,180,40)
        slct_img_btn.setStyleSheet("background-color:#3700B3 ; font : 12px; font-weight: bold ; color : #fff")

        self.window.show() #showing window

    #Styling Window Components
    def __stylingWindowOne (self):
        self.window.setWindowIcon(QtGui.QIcon("home.png"))
        self.window.setWindowTitle("Global Lens")
        self.window.setGeometry(400, 100, 300, 500)  # Samaa
        self.window.setStyleSheet("background-color:#d6d2d2")

        welcome_label = QLabel("Select the Image you want to translate" , self.window)
        welcome_label.setFont(QtGui.QFont("Times", 15, QtGui.QFont.Bold))
        welcome_label.setFixedWidth(300)
        welcome_label.setAlignment(QtCore.Qt.AlignLeft)
        welcome_label.setWordWrap(True)
        welcome_label.setGeometry(20,60,140,140)
        logo_label = QtWidgets.QLabel(self.window)
        logo_label.setGeometry(30, 170, 400, 100)
        logo = QtGui.QPixmap('logo.png')
        logo2 =logo.scaled(250,70)
        logo_label.setPixmap(logo2)
        self.resize(logo.width(),logo.height())

    #Launches image capture window, allows user to take image, then loads it.
    def __take_picture(self) -> None:
        image_file_name = self.imageCaptureDelegate.capture_image()
        global path
        path = image_file_name
        self.ImageWindow()

    #Launches file dialog box, allows user to select an existing image, then loads it.
    def __select_existing_image(self) -> None: 
        file_dialog = QFileDialog()
        image_file_name  = file_dialog.getOpenFileName()
        global path
        path = image_file_name[0]
        self.ImageWindow()
        
    #Creating new Window
    def ImageWindow(self):
        self.window1 = ImageWindow()
        self.window.hide()
예제 #12
0
import os
import subprocess
from telebot import TeleBot
from threading import Event
from config import config
from image_capture import ImageCapture
from motion_monitor import MotionMonitor

root = os.path.dirname(os.path.abspath(__file__))
bot_conf = config(os.path.join(root, 'config.json')).get_config()
telebot = TeleBot(token=bot_conf['TELEGRAM'].get("BOT_API_TOKEN", ""),
                  threaded=False)
messages = bot_conf['TELEGRAM_MESSAGES']
image_capture = ImageCapture()


@telebot.message_handler(commands=['start'])
def start(message):
    telebot.reply_to(message, messages.get("START", ""))


@telebot.message_handler(commands=['help'])
def help(message):
    help = messages.get("HELP", "")
    for h in messages.get("HELP_COMMANDS", []):
        help += "\n    "
        help += h
    telebot.reply_to(message, help)


@telebot.message_handler(commands=['capture'])