i have this script that can create json in this for "{
  "documents": [
    {
      "documentmask": "documentmask in polygon form",
      "lines": [
        {
          "mask": "linemask in polygon form",
          "letters": [
            {
              "mask": "lettermask in polygon form",
              "classification": "the actual letter"
            },
            {
              "mask": "lettermask in polygon form",
              "classification": "the actual letter"
            },
            {
              "mask": "lettermask in polygon form",
              "classification": "the actual letter"
            },"  update me the script "
@app.post("/process_images")
async def process_images(request: Request, selected_images: List[str] = Form(), image_data: str = Form(...)):
    try:
        # Convert base64-encoded image data to bytes and decode
        image_bytes = base64.b64decode(image_data)
        # Convert bytes to PIL Image
        image = Image.open(io.BytesIO(image_bytes))
        # Convert PIL Image to numpy array
        image_array = np.array(image)

        # Convert grayscale image to RGB if necessary
        if len(image_array.shape) == 2:
            image_array = cv2.cvtColor(image_array, cv2.COLOR_GRAY2RGB)

        documents = []

        visualised_origine, instances_lines = run_prediction('model_final_lines', image_array)

        for i, instance_idx in tqdm(enumerate(range(len(instances_lines))), desc="Processing Instances"):
            doc = {"lines": []}

            if instances_lines.pred_classes[instance_idx] != 0:
                documents.append(doc)
                continue

            polygon_lies = instances_lines.pred_masks[instance_idx].cpu().numpy().astype(np.uint8)
            contours_lines, _ = cv2.findContours(polygon_lies, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

            if not contours_lines:
                continue

            for contour_lines_idx, contour_line in enumerate(tqdm(contours_lines)):
                x, y, w, h = cv2.boundingRect(contour_line)
                area = w * h

                # Add a condition based on width and height
                if w < 1000 and h < 40:
                    continue

                mask = np.zeros_like(image_array, dtype=np.uint8)
                cv2.drawContours(mask, [contour_line], contourIdx=-1, color=(255,) * mask.shape[2], thickness=cv2.FILLED)

                #letters = []

                cropped_image = image_array[y:y + h, x:x + w]

                letter_prediction, instance_letter = run_prediction('model_letter', cropped_image)

                for j, instance_jdx in enumerate(range(len(instance_letter))):
                    if instance_letter.pred_classes[j] != 1:
                        continue
                    polygon_letters = instance_letter.pred_masks[j].cpu().numpy().astype(np.uint8)
                    contours_letters, _ = cv2.findContours(polygon_letters, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
                    if not contours_letters:
                        continue

                    letters = []
                    for contour_letter_idx, contour_letter in enumerate(contours_letters):
                        x, y, w, h = cv2.boundingRect(contour_letter)

                        letters.append({
                            "mask_letter": contour_letter.tolist(),  # Convert to list for JSON serialization
                            "classification": classify_character2(cropped_image)
                        })

                    doc["lines"].append({
                    "mask_letter": contour_line.tolist(),  # Convert to list for JSON serialization
                    "letters": letters
                })
                

            documents.append(doc)

        json_data = {
            "JSON": {
                "Doclist": documents
            }
        }

        json_str = json.dumps(json_data)

        with open("data.json", "w") as outfile:
            json.dump(json_data, outfile)

        upload_to_processed_folder(image, "Ayoub1", json_data)

        return JSONResponse(content=json_str, media_type="application/json")