Rotating BBOX by given angle

Viewed 24

I am trying to rotate BBox of Label Studio by some angle 'x'. it is working fine for some annotations but in some it is showing result way worse.

LS Annotation

     {
  "id": 5032,
  "data": {
    "ocr": "/data/upload/27/ff12bef9-BCP_9528.jpg"
  },
  "annotations": [
    {
      "id": 5074,
      "created_username": " knuckle13@gts.ai, 20",
      "created_ago": "1 week, 5 days",
      "result": [
        {
          "original_width": 1832,
          "original_height": 1648,
          "image_rotation": 0,
          "value": {
            "x": 28.652915486588846,
            "y": 46.77020142417043,
            "width": 10.804601238824164,
            "height": 7.594662081257685,
            "rotation": 12.586747669321653
          },
          "id": "_NMJ3S2sRU",
          "from_name": "bbox",
          "to_name": "image",
          "type": "rectangle",
          "origin": "manual"
        },
        {
          "original_width": 1832,
          "original_height": 1648,
          "image_rotation": 0,
          "value": {
            "x": 28.652915486588846,
            "y": 46.77020142417043,
            "width": 10.804601238824164,
            "height": 7.594662081257685,
            "rotation": 12.586747669321653,
            "text": [
              "Plate"
            ]
          },
          "id": "_NMJ3S2sRU",
          "from_name": "transcription",
          "to_name": "image",
          "type": "textarea",
          "origin": "manual"
        },
        {
          "original_width": 1832,
          "original_height": 1648,
          "image_rotation": 0,
          "value": {
            "x": 29.389845656118485,
            "y": 49.45965786007913,
            "width": 1.0134792819827958,
            "height": 3.5450921386229526,
            "rotation": 7.125016348900374
          },
          "id": "sWsYk3ZE3T",
          "from_name": "bbox",
          "to_name": "image",
          "type": "rectangle",
          "origin": "manual"
        },
        {
          "original_width": 1832,
          "original_height": 1648,
          "image_rotation": 0,
          "value": {
            "x": 29.389845656118485,
            "y": 49.45965786007913,
            "width": 1.0134792819827958,
            "height": 3.5450921386229526,
            "rotation": 7.125016348900374,
            "text": [
              "7"
            ]
          },
          "id": "sWsYk3ZE3T",
          "from_name": "transcription",
          "to_name": "image",
          "type": "textarea",
          "origin": "manual"
        },
        {
          "original_width": 1832,
          "original_height": 1648,
          "image_rotation": 0,
          "value": {
            "x": 30.42209307295282,
            "y": 50.0435553887935,
            "width": 1.0134792819827958,
            "height": 3.5450921386229526,
            "rotation": 7.125016348900374
          },
          "id": "AIhI8vTSVr",
          "from_name": "bbox",
          "to_name": "image",
          "type": "rectangle",
          "origin": "manual"
        },
        {
          "original_width": 1832,
          "original_height": 1648,
          "image_rotation": 0,
          "value": {
            "x": 30.42209307295282,
            "y": 50.0435553887935,
            "width": 1.0134792819827958,
            "height": 3.5450921386229526,
            "rotation": 7.125016348900374,
            "text": [
              "P"
            ]
          },
          "id": "AIhI8vTSVr",
          "from_name": "transcription",
          "to_name": "image",
          "type": "textarea",
          "origin": "manual"
        },
        {
          "original_width": 1832,
          "original_height": 1648,
          "image_rotation": 0,
          "value": {
            "x": 31.47985668636047,
            "y": 50.36742263591049,
            "width": 1.0134792819827956,
            "height": 3.545092138622952,
            "rotation": 5.394718292439563
          },
          "id": "heXu9Su5Hk",
          "from_name": "bbox",
          "to_name": "image",
          "type": "rectangle",
          "origin": "manual"
        },
        {
          "original_width": 1832,
          "original_height": 1648,
          "image_rotation": 0,
          "value": {
            "x": 31.47985668636047,
            "y": 50.36742263591049,
            "width": 1.0134792819827956,
            "height": 3.545092138622952,
            "rotation": 5.394718292439563,
            "text": [
              "R"
            ]
          },
          "id": "heXu9Su5Hk",
          "from_name": "transcription",
          "to_name": "image",
          "type": "textarea",
          "origin": "manual"
        },
        {
          "original_width": 1832,
          "original_height": 1648,
          "image_rotation": 0,
          "value": {
            "x": 32.46368253283166,
            "y": 50.61215095338137,
            "width": 1.190466892863956,
            "height": 3.545092138622952,
            "rotation": 3.70035786079211
          },
          "id": "3CSPihE9Sm",
          "from_name": "bbox",
          "to_name": "image",
          "type": "rectangle",
          "origin": "manual"
        },
        {
          "original_width": 1832,
          "original_height": 1648,
          "image_rotation": 0,
          "value": {
            "x": 32.46368253283166,
            "y": 50.61215095338137,
            "width": 1.190466892863956,
            "height": 3.545092138622952,
            "rotation": 3.70035786079211,
            "text": [
              "H"
            ]
          },
          "id": "3CSPihE9Sm",
          "from_name": "transcription",
          "to_name": "image",
          "type": "textarea",
          "origin": "manual"
        },
        {
          "original_width": 1832,
          "original_height": 1648,
          "image_rotation": 0,
          "value": {
            "x": 33.6085387587752,
            "y": 51.09178106625387,
            "width": 1.088342979923151,
            "height": 3.4061513485192694,
            "rotation": 3.7003578607926215
          },
          "id": "ldl0FtSVdj",
          "from_name": "bbox",
          "to_name": "image",
          "type": "rectangle",
          "origin": "manual"
        },
        {
          "original_width": 1832,
          "original_height": 1648,
          "image_rotation": 0,
          "value": {
            "x": 33.6085387587752,
            "y": 51.09178106625387,
            "width": 1.088342979923151,
            "height": 3.4061513485192694,
            "rotation": 3.7003578607926215,
            "text": [
              "0"
            ]
          },
          "id": "ldl0FtSVdj",
          "from_name": "transcription",
          "to_name": "image",
          "type": "textarea",
          "origin": "manual"
        },
        {
          "original_width": 1832,
          "original_height": 1648,
          "image_rotation": 0,
          "value": {
            "x": 34.75339498471874,
            "y": 51.279462414769206,
            "width": 1.0853604571419213,
            "height": 3.678027307764955,
            "rotation": 3.700357860792394
          },
          "id": "f8J6Hf3qYL",
          "from_name": "bbox",
          "to_name": "image",
          "type": "rectangle",
          "origin": "manual"
        },
        {
          "original_width": 1832,
          "original_height": 1648,
          "image_rotation": 0,
          "value": {
            "x": 34.75339498471874,
            "y": 51.279462414769206,
            "width": 1.0853604571419213,
            "height": 3.678027307764955,
            "rotation": 3.700357860792394,
            "text": [
              "7"
            ]
          },
          "id": "f8J6Hf3qYL",
          "from_name": "transcription",
          "to_name": "image",
          "type": "textarea",
          "origin": "manual"
        },
        {
          "original_width": 1832,
          "original_height": 1648,
          "image_rotation": 0,
          "value": {
            "x": 35.804410536404625,
            "y": 51.633971628631485,
            "width": 1.3185874213867632,
            "height": 3.8075471242074705,
            "rotation": 3.700357860792792
          },
          "id": "VTxk-oCec-",
          "from_name": "bbox",
          "to_name": "image",
          "type": "rectangle",
          "origin": "manual"
        },
        {
          "original_width": 1832,
          "original_height": 1648,
          "image_rotation": 0,
          "value": {
            "x": 35.804410536404625,
            "y": 51.633971628631485,
            "width": 1.3185874213867632,
            "height": 3.8075471242074705,
            "rotation": 3.700357860792792,
            "text": [
              "8"
            ]
          },
          "id": "VTxk-oCec-",
          "from_name": "transcription",
          "to_name": "image",
          "type": "textarea",
          "origin": "manual"
        }
      ],
      "was_cancelled": false,
      "ground_truth": false,
      "created_at": "2022-09-12T06:11:28.275515Z",
      "updated_at": "2022-09-12T06:11:28.275577Z",
      "lead_time": 97.278,
      "task": 5032,
      "completed_by": 20,
      "parent_prediction": null,
      "parent_annotation": null
    }
  ],
  "predictions": []
}

What my boss wants

enter image description here

I did tried it but this is result

enter image description here

Output :

BCP_9528.txt,Plate,Plate,525,771,198,126,12.586747669321653 BCP_9528.txt,Character,7,539,816,19,59,7.125016348900374 BCP_9528.txt,Character,P,558,825,19,59,7.125016348900374 BCP_9528.txt,Character,R,577,831,19,59,5.394718292439563 BCP_9528.txt,Character,H,595,835,22,59,3.70035786079211 BCP_9528.txt,Character,0,616,842,20,57,3.7003578607926215 BCP_9528.txt,Character,7,637,846,20,61,3.700357860792394 BCP_9528.txt,Character,8,656,851,25,63,3.700357860792792

Function which output this.

# Rotate function.
def rotate(x,y,xo,yo,theta): #rotate x,y around xo,yo by theta (rad)
    xr=math.cos(theta)*(x-xo)-math.sin(theta)*(y-yo)   + xo
    yr=math.sin(theta)*(x-xo)+math.cos(theta)*(y-yo)  + yo
    return [xr,yr]

    # Loop over given json and update
            # Polygon to pixels because label studio uses percentage instead of pixles.
            for percentageConversion in hold_data:
                # Only proceed if given result
                # value have text in it.
                if 'text' in percentageConversion['value']:
                    min_width = math.ceil((percentageConversion['value']['x'] * percentageConversion['original_width']) / 100)
                    min_height = math.ceil((percentageConversion['value']['y'] * percentageConversion['original_height']) / 100)
                    max_width = math.ceil((percentageConversion['value']['width'] * percentageConversion['original_width']) / 100)
                    max_height = math.ceil((percentageConversion['value']['height'] * percentageConversion['original_height']) / 100)

                    # Append data to the container.
                    data_d.append([min_width, min_height, max_width, max_height, percentageConversion['value']['text'][0], percentageConversion['value']['rotation']])

            for dataset in data_d:
                # Push given value of text with
                # coordinates.
                with open(image_filename.replace('.jpg', '.txt'), 'a') as f:
                    f.write('\n{file_name},{content},{type},{x},{y},{x1},{y1},{rotation}'.format(file_name=image_filename.replace('.jpg', '.txt'), content='Plate' if dataset[4] == 'Plate' else 'Character', type=dataset[4], x=int(dataset[0]), y=int(dataset[1]), x1=int(dataset[2]), y1=int(dataset[3]), rotation=dataset[5]))

0 Answers
Related