예제 #1
0
파일: index.py 프로젝트: jroes/tsurp
    def post(self):
        try:
            sender = self.request.GET.get("from", "")
            if not sender or sender == '':
                self.error(500)
                return

            recipient = self.request.GET.get("to", "")
            if recipient == ADMIN_ADDRESS:
                mail.send_mail(sender=CONTACT_ADDRESS,
                               to=ADMIN_ADDRESS,
                               subject="mail to [email protected] from %s" % sender,
                               body=self.request.body)
                return
                
            message = email.message_from_string(self.request.body)

            for part in message.walk():
                if part.get_content_maintype() == 'multipart':
                    continue

                filename = part.get_filename()
                if filename and filename not in BAD_FILES:
                    ext = mimetypes.guess_extension(part.get_content_type())
                
                    if ext in ALLOWED_EXTENSIONS:
                        image = db.Blob(part.get_payload(decode=True))

                        ext = ext.replace('.', '')
                        # turn .jpe's into jpgs, there's no content/type: jpe
                        if ext == 'jpe': ext = 'jpg'
                        
                        image_id = ImageStore.add(image, ext=ext,
                                                  author=users.User(sender))

                        url = IMAGE_URL % image_id
                        mail.send_mail(sender=CONTACT_ADDRESS,
                                       to=sender,
                                       subject="tsurp: %s" % url,
                                       body=url)
                        logging.debug('sent image url %s to %s' % (url, sender))
                    else:
                        logging.debug('invalid ext for %s: %s' % (filename,
                                                                  ext))

        except apiproxy_errors.OverQuotaError, e:
            self.error(500)
            logging.debug('lost message for %s, over quota: %s.' % (sender, e))
예제 #2
0
파일: index.py 프로젝트: jroes/tsurp
    def post(self):
        try:
            if not isinstance(self.request.get('img'), str):
                self.redirect('/')
            image = db.Blob(self.request.get('img'))
            ext = self.request.POST.get('img').filename.split('.')[-1]
            author = users.get_current_user()
            title = self.request.get('title')

            image_id = ImageStore.add(image, ext=ext,
                                      author=author, title=title)

            self.redirect('/' + image_id)
        except Exception, e:
            self.error(500)
            logging.error('%s: %s' % (self.__class__.__name__,
                                     traceback.print_exc()))
예제 #3
0
def ocr(input_: io.BytesIO) -> Optional[float]:
    input_.seek(0)

    global _timestamp
    _timestamp = datetime.utcnow().strftime("%Y_%m_%d-%H_%M_%S")
    with ImageStore(_config) as img_store:
        image = cv2.imdecode(np.fromstring(input_.read(), np.uint8), 1)
        img_store.add_image(image, "original")

        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        img_store.add_image(gray, "gray")
        del image

        rotated = _rotate(_config.image_rotation, gray)
        img_store.add_image(rotated, "rotated")
        del gray

        blurred = cv2.GaussianBlur(
            rotated, (_config.blur_ksize_width, _config.blur_ksize_height), 0)
        img_store.add_image(rotated, "blurred")
        del rotated

        edged = _get_edged(blurred)

        if _config.positions:
            logger.info("config has pre-defined positions, no auto detection")
            sorted_aligned_bb = _use_positions()
        else:
            logger.info("detecting positions of numbers automatically.")

            sorted_aligned_bb = _auto_detect_numbers(edged, img_store)

        del edged

        numbers_as_text = image_to_text(blurred, sorted_aligned_bb, img_store)
        del blurred

        final_result = ocr_result_to_value(numbers_as_text)

        logger.info("the detected result is: {final_result}",
                    final_result=final_result)

        return final_result