Sugere um laboratório para experimentação com imagens de forma inteligente. modelo de software em Python que utilize a API da OpenAI e a infraestrutura da Oracle Cloud para tratamento, classificação e identificação de imagens. O código será estruturado em classes para facilitar a organização e a reutilização.
AI Image Processing Project with OpenAI API and Oracle Cloud Overview This project aims to develop a software application for image processing, classification, and identification using the OpenAI API and Oracle Cloud infrastructure. The application will leverage advanced machine learning techniques and will be structured in classes for better organization and reuse.
Key Features Image Processing: Use the OpenAI API to process images effectively. Image Classification: Classify images based on predefined categories. Image Identification: Identify specific objects or patterns within images. Cloud Integration: Utilize Oracle Cloud for storage and processing capabilities. Robust Error Handling: Implement comprehensive error handling to ensure application stability.
Infrastructure Setup The infrastructure will be provisioned using Terraform on Oracle Cloud Infrastructure (OCI).
Terraform Configuration Create a Directory:
bash mkdir ai-image-processing cd ai-image-processing Create Terraform Files:
main.tf: Define your infrastructure components (compute instances, networking, etc.). variables.tf: Specify variables for flexibility and customization. outputs.tf: Define outputs for easier access to resource information. Terraform Script Example:
hcl
provider "oci" { region = var.region }
resource "oci_core_instance" "instance" { availability_domain = var.availability_domain compartment_id = var.compartment_id display_name = "AI-Image-Processing-Instance" shape = var.shape
create_vnic_details { subnet_id = var.subnet_id }
source_details { source_type = "image" source_id = var.image_id } } Create terraform.tfvars:
hcl
region = "us-phoenix-1" availability_domain = "Phx2" compartment_id = "ocid1.compartment.oc1..example" shape = "VM.Standard2.1" subnet_id = "ocid1.subnet.oc1.phx.example" image_id = "ocid1.image.oc1.phx.example" Steps to Set Up the Infrastructure Install Terraform: Installation Instructions.
Configure OCI: Ensure your ~/.oci/config file is correctly set up.
Navigate to the Terraform Directory:
bash cd terraform Initialize Terraform:
bash terraform init Plan the Infrastructure:
bash terraform plan Apply the Configuration:
bash terraform apply Software Usage Install Dependencies:
bash pip install -r requirements.txt Run the Application:
bash python src/main.py License This project is licensed under the MIT License. See the LICENSE file for more information.
Final Considerations Dependencies: Ensure to create a requirements.txt file with necessary dependencies, such as requests, torch, Pillow, torchvision, etc. Models: Paths for the models (if using pre-trained models) should be adjusted based on your implementation. Testing: It is recommended to test each component of the system in isolation before integrating them.
###############################This README is a starting point. Feel free to expand it as necessary.#####################################################################
Estrutura do Diretório do Projeto
ImageProcessingOpenAI/ │ ├── terraform/ │ ├── main.tf │ ├── variables.tf │ ├── outputs.tf │ ├── terraform.tfvars │ ├── src/ │ ├── init.py │ ├── main.py │ ├── image_processor.py │ ├── image_classifier.py │ ├── image_identifier.py │ ├── image_storage.py │ └── README.md
- Infraestrutura: Terraform main.tf hcl provider "oci" {
config_file = "~/.oci/config" profile = "DEFAULT" # Change to your profile name if necessary }
resource "oci_core_virtual_cloud_network" "vcn" { compartment_id = var.compartment_id display_name = "ImageProcessingVCN" cidr_block = "10.0.0.0/16" }
resource "oci_core_subnet" "public_subnet" { compartment_id = var.compartment_id vcn_id = oci_core_virtual_cloud_network.vcn.id display_name = "PublicSubnet" cidr_block = "10.0.1.0/24" dhcp_options_id = oci_core_virtual_cloud_network.vcn.default_dhcp_options_id availability_domain = var.availability_domain prohibit_public_ip_on_vnic = false }
resource "oci_core_subnet" "private_subnet" { compartment_id = var.compartment_id vcn_id = oci_core_virtual_cloud_network.vcn.id display_name = "PrivateSubnet" cidr_block = "10.0.2.0/24" dhcp_options_id = oci_core_virtual_cloud_network.vcn.default_dhcp_options_id availability_domain = var.availability_domain prohibit_public_ip_on_vnic = true }
resource "oci_core_instance" "app_instance" { availability_domain = var.availability_domain compartment_id = var.compartment_id display_name = "ImageProcessingInstance" shape = "VM.Standard2.1" # Choose the shape based on your needs
create_vnic_details { subnet_id = oci_core_subnet.public_subnet.id assign_public_ip = true }
metadata = { ssh_authorized_keys = file(var.ssh_public_key_path) } }
output "public_ip" { value = oci_core_instance.app_instance.public_ip }
output "instance_id" { value = oci_core_instance.app_instance.id }
variables.tf hcl variable "compartment_id" { description = "The OCID of the compartment" }
variable "availability_domain" { description = "The availability domain to create resources in" }
variable "ssh_public_key_path" { description = "Path to your SSH public key" }
outputs.tf hcl output "public_ip" { description = "Public IP of the instance" value = oci_core_instance.app_instance.public_ip }
output "instance_id" { description = "ID of the instance" value = oci_core_instance.app_instance.id }
terraform.tfvars hcl compartment_id = "ocid_do_seu_compartimento" availability_domain = "AD-1" # Substitua pelo seu AD ssh_public_key_path = "~/.ssh/id_rsa.pub" # O caminho para a sua chave SSH pública
- Código do Aplicativo em Python src/init.py python C
src/image_processor.py python import requests from PIL import Image import io
class ImageProcessor: def init(self, openai_api_key): self.openai_api_key = openai_api_key
def generate_image(self, prompt):
url = "https://api.openai.com/v1/images/generations"
headers = {
"Authorization": f"Bearer {self.openai_api_key}",
"Content-Type": "application/json",
}
data = {
"prompt": prompt,
"n": 1,
"size": "1024x1024",
}
response = requests.post(url, headers=headers, json=data)
image_url = response.json()['data'][0]['url']
return self.download_image(image_url)
def download_image(self, url):
response = requests.get(url)
image = Image.open(io.BytesIO(response.content))
return image
def preprocess_image(self, image, size=(224, 224)):
return image.resize(size)
src/image_classifier.py python import torch from torchvision import models, transforms
class ImageClassifier: def init(self, model_path): self.model = self.load_model(model_path)
def load_model(self, model_path):
model = models.resnet50(pretrained=True)
model.eval()
return model
def classify(self, image):
preprocess = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
input_tensor = preprocess(image)
input_batch = input_tensor.unsqueeze(0) # Cria um mini-batch como esperado pela rede
with torch.no_grad():
output = self.model(input_batch)
return output.argmax().item() # Retorna a classe com maior pontuação
src/image_identifier.py python import torch
class ImageIdentifier: def init(self, model_path): self.model = self.load_model(model_path)
def load_model(self, model_path):
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
model.eval()
return model
def identify(self, image):
results = self.model(image)
return results.xyxy[0] # Retorna resultados com coordenadas das caixas delimitadoras
src/image_storage.py python from oracleai import Cloud import io
class ImageStorage: def init(self, oracle_cloud_credentials): self.cloud = Cloud(oracle_cloud_credentials)
def upload_image(self, image, image_name):
# Assume que 'image' é um objeto PIL Image
with io.BytesIO() as output:
image.save(output, format='JPEG')
image_data = output.getvalue()
# Código para upload para OCI
self.cloud.upload(image_name, image_data)
def download_image(self, image_id):
# Código para download da imagem do OCI usando o image_id
return self.cloud.download(image_id)
src/main.py python from image_processor import ImageProcessor from image_classifier import ImageClassifier from image_identifier import ImageIdentifier from image_storage import ImageStorage
if name == "main": OPENAI_API_KEY = 'sua-chave-da-api'(a minha nao passo!!) ORACLE_CLOUD_CREDENTIALS = 'suas-credenciais'
processor = ImageProcessor(OPENAI_API_KEY)
classifier = ImageClassifier('model_path')
identifier = ImageIdentifier('model_path')
storage = ImageStorage(ORACLE_CLOUD_CREDENTIALS)
# Gerar e processar uma imagem
generated_image = processor.generate_image("A beautiful landscape")
processed_image = processor.preprocess_image(generated_image)
# Classificar a imagem
class_id = classifier.classify(processed_image)
print(f'Class ID: {class_id}')
# Identificar objetos na imagem
detections = identifier.identify(processed_image)
print(f'Detections: {detections}')
# Fazer upload da imagem para Oracle Cloud
storage.upload_image(generated_image, 'landscape_image.jpg')
3.Condieracoes Finais
Este projeto é um software de inteligência artificial que utiliza a API da OpenAI para gerar, classificar e identificar imagens. Ele é projetado para ser hospedado na Oracle Cloud usando Terraform para a infraestrutura.
ImageProcessingOpenAI/ │ ├── terraform/ │ ├── main.tf │ ├── variables.tf │ ├── outputs.tf │ ├── terraform.tfvars │ ├── src/ │ ├── init.py │ ├── main.py │ ├── image_processor.py │ ├── image_classifier.py │ ├── image_identifier.py │ ├── image_storage.py │ └── README.md
A infraestrutura é provisionada usando Terraform. Antes de executar o Terraform, configure seu arquivo de configuração da Oracle Cloud.
-
Instalar o Terraform: Instruções de instalação.
-
Configurar OCI: Certifique-se de que seu arquivo
~/.oci/config
está corretamente configurado. -
Navegar até o diretório do Terraform:
cd terraform
Inicializar o Terraform:
bash terraform init Preencher as variáveis: Crie um arquivo terraform.tfvars com os valores apropriados.
Planejar a infraestrutura:
bash terraform plan Aplicar a configuração:
bash terraform apply Uso do Software Instalar Dependências:
bash pip install -r requirements.txt Executar o Aplicativo:
bash python src/main.py Licença Este projeto está licenciado sob a MIT License. Veja o arquivo LICENSE para mais informações.
-
Dependências: Certifique-se de criar um arquivo
requirements.txt
com as dependências necessárias, comorequests
,torch
,Pillow
,torchvision
, etc. -
Modelos: Os caminhos para os modelos (se estiver usando modelos pré-treinados) devem ser ajustados conforme sua implementação.
-
Testes: É recomendável testar cada componente do sistema de forma isolada antes de integrá-los.
-
Documentação: Este README é um ponto de partida. Sinta-se à vontade para expandi-lo conforme necessário.
Esse guia fornece uma visão completa de como montar o projeto e a infraestrutura necessária para