コード例 #1
0
ファイル: jobs.py プロジェクト: dzwiedziu-nkg/credo-webapp
def relabel_detections(start_id, limit):
    detections = Detection.objects.filter(id__gte=start_id).filter(
        id__lt=start_id + limit
    )
    r = get_redis_connection()

    for d in detections:
        s = True

        if d.source != "api_v2" or not d.frame_content:
            s = False

        if s and not validate_image(d.frame_content):
            s = False

        if s:
            start_time = r.zscore(cache.make_key("start_time"), d.user_id)
            if start_time:
                s = d.timestamp > start_time
            else:
                s = False

        if s != d.visible:
            d.visible = s
            d.save()
コード例 #2
0
ファイル: handlers.py プロジェクト: GELATICA/credo-webapp
def handle_detection_frame(frame):
    key = frame.body.user_info.key
    user_info = frame.body.user_info

    user = authenticate(token=key)

    # create or get team
    team_name = user_info.team
    team, _ = Team.objects.get_or_create(name=team_name)

    if not team == user.team:
        user.team = team
        user.save()

    if user is None:
        logger.info("Unauthorized detection submission.")
        raise UnauthorizedException("Wrong username or password!")

    device_info = frame.body.device_info

    device, _ = Device.objects.get_or_create(
        device_id=device_info.deviceId,
        device_model=device_info.deviceModel,
        system_version=device_info.androidVersion,
        user=user
    )

    detection_info = frame.body.detection_info
    decoded_image = None
    visible = False

    if detection_info.frame_content:
        decoded_image = base64.decodestring(detection_info.frame_content)
        if validate_image(decoded_image):
            visible = False
        else:
            visible = True

    detection = Detection.objects.create(
        accuracy=detection_info.accuracy,
        altitude=detection_info.altitude,
        frame_content=decoded_image,
        height=detection_info.height,
        width=detection_info.width,
        d_id=detection_info.id,
        latitude=detection_info.latitude,
        longitude=detection_info.longitude,
        provider=detection_info.provider,
        timestamp=detection_info.timestamp,
        source='api_v1',
        device=device,
        user=user,
        team=user.team,
        visible=visible
    )

    logger.info("Stored detection for user %s, visibility: %s." % (user.display_name, visible))
コード例 #3
0
    def handle(self, *args, **options):
        detections = Detection.objects.all()

        for d in detections:
            if (not d.frame_content) or validate_image(d.frame_content):
                self.stdout.write("Processing image %s" % d.id)
                d.visible = False
                d.save()

        self.stdout.write("Done!")
コード例 #4
0
    def handle(self, *args, **options):
        detections = Detection.objects.all()

        for d in detections:
            if d.frame_content:
                d.brightness = rate_brightness(d.frame_content)
                d.save()
            if (not d.frame_content) or validate_image(d.frame_content):
                self.stdout.write("Hiding detection %s (image validation failed)" % d.id)
                d.visible = False
                d.save()
            if abs(d.time_received - d.timestamp) > 3600 * 24 * 365 * 5 * 1000:
                self.stdout.write("Hiding detection %s (invalid date)" % d.id)
                d.visible = False
                d.save()

        self.stdout.write("Done!")
コード例 #5
0
def handle_detection(request):
    serializer = DetectionRequestSerializer(data=request.data)
    if not serializer.is_valid():
        raise CredoAPIException(str(serializer.errors))
    vd = serializer.validated_data
    detections = []
    for d in vd['detections']:

        frame_content = base64.b64decode(d['frame_content'])
        visible = True
        if (not frame_content) or validate_image(frame_content):
            visible = False

        detections.append(
            Detection.objects.create(accuracy=d['accuracy'],
                                     altitude=d['altitude'],
                                     frame_content=frame_content,
                                     height=d['height'],
                                     width=d['height'],
                                     d_id=d['id'],
                                     latitude=d['latitude'],
                                     longitude=d['longitude'],
                                     provider=d['provider'],
                                     timestamp=d['timestamp'],
                                     source='api_v2',
                                     device=Device.objects.get_or_create(
                                         device_id=vd['device_id'],
                                         device_type=vd['device_type'],
                                         device_model=vd['device_model'],
                                         system_version=vd['system_version'],
                                         user=request.user)[0],
                                     user=request.user,
                                     team=request.user.team,
                                     visible=visible))
    data = {
        'detections': [
            {
                'id': d.id  # TODO: Should we send more data?
            } for d in detections
        ]
    }
    logger.info('Stored {} detections for user {}'.format(
        len(detections), request.user))
    return data
コード例 #6
0
def handle_detection(request):
    try:
        serializer = DetectionRequestSerializer(data=request.data)
    except ParseError:
        raise CredoAPIException(
            "Could not parse request body as a valid JSON object")

    if not serializer.is_valid():
        raise CredoAPIException(str(serializer.errors))
    vd = serializer.validated_data

    detections = []
    r = None
    for d in vd["detections"]:

        frame_content = base64.b64decode(d["frame_content"])
        visible = True
        if (not frame_content) or (not validate_image(frame_content)):
            visible = False

        if visible:
            if not r:
                r = get_redis_connection(write=False)
            start_time = r.zscore(cache.make_key("start_time"),
                                  request.user.id)
            if start_time:
                visible = d["timestamp"] > start_time
            else:
                visible = False

        if visible and d["x"] is not None:
            r = get_redis_connection()
            if not r.sadd(
                    cache.make_key("pixels_{}".format(request.user.id)),
                    "{} {}".format(d["x"], d["y"]),
            ):
                visible = False

        detections.append(
            Detection.objects.create(
                accuracy=d["accuracy"],
                altitude=d["altitude"],
                frame_content=frame_content,
                height=d["height"],
                width=d["width"],
                x=d["x"],
                y=d["y"],
                latitude=d["latitude"],
                longitude=d["longitude"],
                provider=d["provider"],
                timestamp=d["timestamp"],
                metadata=d["metadata"],
                source="api_v2",
                device=Device.objects.get_or_create(
                    device_id=vd["device_id"],
                    device_type=vd["device_type"],
                    device_model=vd["device_model"],
                    system_version=vd["system_version"],
                    user=request.user,
                )[0],
                user=request.user,
                team=request.user.team,
                visible=visible,
            ))
    data = {"detections": [{"id": d.id} for d in detections]}
    recalculate_user_stats.delay(request.user.id)
    recalculate_team_stats.delay(request.user.team.id)
    logger.info("Stored {} detections for user {}".format(
        len(detections), request.user))
    return data