File size: 1,838 Bytes
bc31a94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import numpy as np
import PIL.Image
import os
import requests
import io

class ImageProcessor:
    def __init__(self):
        self.temp_dir = "temp"
        os.makedirs(self.temp_dir, exist_ok=True)

    def save_uploaded_file(self, uploaded_file):
        """Save uploaded file and return path"""
        image_path = os.path.join(self.temp_dir, "temp_uploaded_image.png")
        with open(image_path, "wb") as f:
            f.write(uploaded_file.getbuffer())
        return image_path

    def download_image(self, image_url):
        """Download image from URL and return path"""
        try:
            response = requests.get(image_url)
            if response.status_code == 200:
                image_path = os.path.join(self.temp_dir, "temp_url_image.png")
                img = PIL.Image.open(io.BytesIO(response.content))
                img.save(image_path)
                return image_path
        except Exception as e:
            print(f"Error downloading image: {e}")
            return None

    def preprocess_image(self, image_path):
        """Preprocess image for model input"""
        try:
            img = PIL.Image.open(image_path)
            img_resized = img.resize((224, 224))
            img_array = np.array(img_resized)
            img_normalized = img_array / 255.0
            img_input = np.expand_dims(img_normalized, axis=0)
            return img_input
        except Exception as e:
            print(f"Error preprocessing image: {e}")
            return None

    def cleanup(self):
        """Clean up temporary files"""
        for file in os.listdir(self.temp_dir):
            try:
                os.remove(os.path.join(self.temp_dir, file))
            except Exception as e:
                print(f"Error cleaning up file {file}: {e}")