Back

How can i run this cloud function in python.

  • 0
  • General
  • Functions
Rovar2000
10 Mar, 2024, 05:43

i'm kinda new in python and i wanna know why i can't run this cloud function in my self-hosted appwrite cloud function. this is my code for image processing, it works on locale runs but i don't know how to make it work on cloud.

TypeScript
import cv2
import dlib
import numpy as np
import pytesseract
from PIL import Image


# Load the detector
detector = dlib.get_frontal_face_detector()

def extract_face(image_path, output_path, padding=35):
    # Load the image
    img = cv2.imread(image_path)
    # Convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    # Use the detector to find faces
    faces = detector(gray)
    for i, face in enumerate(faces):
        # Extract the face with some padding to make the image larger
        x1, y1, x2, y2 = face.left() - padding, face.top() - padding, face.right() + padding, face.bottom() + padding
        # Ensure the coordinates are within the image boundaries
        x1, y1 = max(0, x1), max(0, y1)
        x2, y2 = min(img.shape[1]-1, x2), min(img.shape[0]-1, y2)
        # Crop the face out of the image
        crop = img[y1:y2, x1:x2]
        return crop

In the console in the appwrite self-host it logs Docker Error: tar: short read

TL;DR
Developers are facing issues running a cloud function in self-hosted Appwrite. The issue might be due to an error in the Docker build process. The provided code for image processing seems to work in local runs but not on the cloud. Check the Docker logs for errors like "tar: short read" which could be causing the problem.
Rovar2000
10 Mar, 2024, 05:44

also part 2 of the code:

TypeScript
def passportProcessing(image_path):
   # Reading the image
    img = cv2.imread(image_path)

    # Convert to gray scale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    # Adaptive thresholding
    adaptive_thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
                                            cv2.THRESH_BINARY, 11, 2)

    # Morphological operations to enhance text
    kernel = np.ones((1, 1), np.uint8)
    img_morph = cv2.morphologyEx(adaptive_thresh, cv2.MORPH_OPEN, kernel)
    img_morph = cv2.morphologyEx(img_morph, cv2.MORPH_CLOSE, kernel)

    if img is not None:
        text = pytesseract.image_to_string(img_morph,lang='eng+ara')
        texts = text.split('\n')
        # Applying the cleaning function to each text and filtering out single character or empty entries
        filtered_texts_corrected = [clean_text(text) for text in texts if text.strip() and len(text.strip()) > 1]

        return filtered_texts_corrected
    else:
        print("Image not loaded correctly. Check the file path and file format: {image_path}")
        
def clean_text(text):
    return ''.join(char for char in text if char not in ('\u200f', '\u200e'))

def main(req, res):
  client = Client()
  
  print('Hello from Appwrite Function!')
  print("Variables",req.variables)

  # You can remove services you don't use
  database = Databases(client)
  users = Users(client)

  if not req.variables.get('APPWRITE_FUNCTION_ENDPOINT') or not req.variables.get('APPWRITE_FUNCTION_API_KEY'):
    print('Environment variables are not set. Function cannot use Appwrite SDK.')
  else:
    (
    client
      .set_endpoint(req.variables.get('APPWRITE_FUNCTION_ENDPOINT', None))
      .set_project(req.variables.get('APPWRITE_FUNCTION_PROJECT_ID', None))
      .set_key(req.variables.get('APPWRITE_FUNCTION_API_KEY', None))
      .set_self_signed(True)
    )
  
  return res.json({
    "areDevelopersAwesome": True,
  })
Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more