import gradio as gr import numpy as np import cv2 from PIL import Image import json import os from datetime import datetime import mediapipe as mp class ARVRMarketingSystem: def __init__(self): self.user_preferences = {} self.product_database = {} self.feedback_data = [] # Initialize AR components self.mp_face_mesh = mp.solutions.face_mesh self.face_mesh = self.mp_face_mesh.FaceMesh( static_image_mode=True, max_num_faces=1, min_detection_confidence=0.5 ) self.aruco_dict = cv2.aruco.getPredefinedDictionary(cv2.aruco.DICT_4X4_250) self.aruco_params = cv2.aruco.DetectorParameters() self.aruco_detector = cv2.aruco.ArucoDetector(self.aruco_dict, self.aruco_params) def load_preferences(self, user_id): # Simulate loading user preferences self.user_preferences = { "ar_enabled": True, "vr_quality": "high", "preferred_categories": ["furniture", "clothing"] } return json.dumps(self.user_preferences, indent=2) def process_product_upload(self, image, product_name, description, category): if image is None: return "Error: No image provided", None try: # Add product to database timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") product_id = f"PROD_{timestamp}" self.product_database[product_id] = { "name": product_name, "description": description, "category": category, "ar_ready": True, "image": image } return f"Product {product_name} successfully uploaded with ID: {product_id}", image except Exception as e: return f"Error processing product: {str(e)}", None def generate_ar_preview(self, image, product_id, category): """Generate AR preview based on product category""" if image is None: return None, "No image provided" try: # Convert to OpenCV format image_np = np.array(image) image_rgb = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB) # Generate preview based on category if category == "glasses": return self._generate_glasses_preview(image_rgb) elif category == "furniture": return self._generate_furniture_preview(image_rgb) else: return self._generate_basic_preview(image_rgb) except Exception as e: return None, f"Error generating preview: {str(e)}" def _generate_glasses_preview(self, image): """Generate AR preview for glasses""" results = self.face_mesh.process(image) if not results.multi_face_landmarks: return image, "No face detected" face_landmarks = results.multi_face_landmarks[0] overlay = image.copy() # Get eye positions left_eye = np.mean([[face_landmarks.landmark[p].x * image.shape[1], face_landmarks.landmark[p].y * image.shape[0]] for p in [33, 133, 157, 158, 159, 160, 161, 246]], axis=0) right_eye = np.mean([[face_landmarks.landmark[p].x * image.shape[1], face_landmarks.landmark[p].y * image.shape[0]] for p in [362, 263, 384, 385, 386, 387, 388, 466]], axis=0) # Calculate glasses dimensions eye_distance = np.linalg.norm(right_eye - left_eye) frame_width = int(eye_distance * 1.5) frame_height = int(frame_width * 0.4) # Draw glasses frames cv2.ellipse(overlay, (int(left_eye[0]), int(left_eye[1])), (int(frame_width//4), int(frame_height//2)), 0, 0, 360, (0, 0, 0), 2) cv2.ellipse(overlay, (int(right_eye[0]), int(right_eye[1])), (int(frame_width//4), int(frame_height//2)), 0, 0, 360, (0, 0, 0), 2) # Draw bridge cv2.line(overlay, (int(left_eye[0] + frame_width//4), int(left_eye[1])), (int(right_eye[0] - frame_width//4), int(right_eye[1])), (0, 0, 0), 2) output = cv2.addWeighted(image, 0.7, overlay, 0.3, 0) return output, "Glasses AR preview generated" def _generate_furniture_preview(self, image): """Generate AR preview for furniture""" corners, ids, rejected = self.aruco_detector.detectMarkers(image) if ids is None: gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) edges = cv2.Canny(gray, 50, 150) lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, minLineLength=100, maxLineGap=10) output = image.copy() if lines is not None: for line in lines: x1, y1, x2, y2 = line[0] cv2.line(output, (x1, y1), (x2, y2), (0, 255, 0), 2) center_x = image.shape[1] // 2 center_y = image.shape[0] // 2 cv2.rectangle(output, (center_x - 50, center_y - 50), (center_x + 50, center_y + 50), (0, 0, 255), 2) return output, "Floor plane estimated (no markers detected)" output = cv2.aruco.drawDetectedMarkers(image.copy(), corners, ids) if len(corners) > 0: for corner in corners: center = np.mean(corner[0], axis=0, dtype=np.int32) cv2.rectangle(output, (int(center[0]) - 50, int(center[1]) - 50), (int(center[0]) + 50, int(center[1]) + 50), (0, 0, 255), 2) return output, "Furniture AR preview generated" def _generate_basic_preview(self, image): """Generate basic AR preview for other categories""" output = image.copy() height, width = output.shape[:2] cv2.putText(output, "AR Preview", (width//2 - 50, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) for i in range(3): x = width // 4 * (i + 1) y = height // 2 cv2.circle(output, (x, y), 5, (0, 255, 0), -1) cv2.putText(output, f"Info {i+1}", (x - 20, y - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1) return output, "Basic AR preview generated" def collect_feedback(self, product_id, rating, feedback_text): feedback_entry = { "product_id": product_id, "rating": rating, "feedback": feedback_text, "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S") } self.feedback_data.append(feedback_entry) return f"Feedback collected successfully for product {product_id}" def get_analytics(self): analytics = { "total_products": len(self.product_database), "ar_interactions": np.random.randint(100, 1000), "vr_sessions": np.random.randint(50, 500), "avg_rating": round(np.random.uniform(3.5, 5.0), 2), "total_feedback": len(self.feedback_data) } return json.dumps(analytics, indent=2) def create_interface(): system = ARVRMarketingSystem() with gr.Blocks(theme=gr.themes.Soft()) as interface: gr.Markdown("# AR/VR Marketing Tools Dashboard") with gr.Tab("System Initialization"): user_id_input = gr.Textbox(label="User ID") load_btn = gr.Button("Load User Preferences") preferences_output = gr.JSON(label="User Preferences") load_btn.click(system.load_preferences, inputs=user_id_input, outputs=preferences_output) with gr.Tab("Product Management"): with gr.Row(): with gr.Column(): product_image = gr.Image(label="Upload Product Image") product_name = gr.Textbox(label="Product Name") product_desc = gr.Textbox(label="Product Description") product_category = gr.Dropdown( choices=["glasses", "furniture", "clothing", "accessories"], label="Product Category" ) upload_btn = gr.Button("Upload Product") with gr.Column(): upload_output = gr.Textbox(label="Upload Status") preview_image = gr.Image(label="Processed Image") upload_btn.click( system.process_product_upload, inputs=[product_image, product_name, product_desc, product_category], outputs=[upload_output, preview_image] ) with gr.Tab("AR/VR Preview"): with gr.Row(): with gr.Column(): preview_image_input = gr.Image(label="Input Image") preview_product_id = gr.Textbox(label="Product ID") preview_category = gr.Dropdown( choices=["glasses", "furniture", "clothing", "accessories"], label="Product Category" ) generate_preview_btn = gr.Button("Generate AR Preview") with gr.Column(): preview_output_image = gr.Image(label="AR Preview") preview_status = gr.Textbox(label="Preview Status") generate_preview_btn.click( system.generate_ar_preview, inputs=[preview_image_input, preview_product_id, preview_category], outputs=[preview_output_image, preview_status] ) with gr.Tab("Feedback & Analytics"): with gr.Row(): with gr.Column(): feedback_product_id = gr.Textbox(label="Product ID") feedback_rating = gr.Slider(minimum=1, maximum=5, step=0.5, label="Rating") feedback_text = gr.Textbox(label="Feedback") submit_feedback_btn = gr.Button("Submit Feedback") feedback_output = gr.Textbox(label="Feedback Status") with gr.Column(): analytics_btn = gr.Button("View Analytics") analytics_output = gr.JSON(label="System Analytics") submit_feedback_btn.click( system.collect_feedback, inputs=[feedback_product_id, feedback_rating, feedback_text], outputs=feedback_output ) analytics_btn.click(system.get_analytics, outputs=analytics_output) return interface # Launch the interface if __name__ == "__main__": demo = create_interface() demo.launch(share=True)