From b777d0b6a6844b31aa916b0c8817871ad2899527 Mon Sep 17 00:00:00 2001 From: RianaAzad <20303033@iubat.edu> Date: Sat, 8 Mar 2025 22:58:08 +0600 Subject: [PATCH] FEAT: implement camera component for taking realtime pictures --- package.json | 2 +- src/App.tsx | 236 +++++++++++++++++++++----------------- src/components/Camera.tsx | 133 +++++++++++++++++++++ 3 files changed, 267 insertions(+), 104 deletions(-) create mode 100644 src/components/Camera.tsx diff --git a/package.json b/package.json index cc5da19..f84ca3b 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "version": "0.0.0", "type": "module", "scripts": { - "dev": "vite", + "dev": "vite --host", "build": "vite build", "lint": "eslint .", "preview": "vite preview" diff --git a/src/App.tsx b/src/App.tsx index ffeb485..ee72d40 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -2,6 +2,7 @@ import React, { useState, useCallback, useEffect } from "react"; import { useDropzone } from "react-dropzone"; import { Images } from "./components/Images"; import { processImages, initializeModel, getModelInfo } from "../lib/process"; +import { Camera } from "./components/Camera"; interface AppError { message: string; @@ -45,6 +46,7 @@ export default function App() { const [currentModel, setCurrentModel] = useState<'briaai/RMBG-1.4' | 'Xenova/modnet'>('briaai/RMBG-1.4'); const [isModelSwitching, setIsModelSwitching] = useState(false); const [images, setImages] = useState([]); + const [showCamera, setShowCamera] = useState(false); useEffect(() => { if (isMobileSafari() && !isRedirectPage()) { @@ -180,6 +182,13 @@ export default function App() { }, }); + // Add this function to handle camera capture + const handleCameraCapture = async (capturedImage: Blob) => { + const file = new File([capturedImage], `camera-${Date.now()}.jpg`, { type: 'image/jpeg' }); + onDrop([file]); + setShowCamera(false); + }; + // Remove the full screen error and loading states return ( @@ -216,116 +225,137 @@ export default function App() {
-
- {images.length === 0 && ( -
-
-

- Remove Background -
- In One Click -

-

- Transform your images instantly with AI-powered background removal -

-
    - {[ - '100% Free & Private', - 'Browser-based Processing', - 'Advanced Background Editing' - ].map((feature, index) => ( -
  • - - - - {feature} -
  • - ))} -
+ {showCamera ? ( + setShowCamera(false)} + /> + ) : ( +
+ {images.length === 0 && ( +
+
+

+ Remove Background +
+ In One Click +

+

+ Transform your images instantly with AI-powered background removal +

+
    + {[ + '100% Free & Private', + 'Browser-based Processing', + 'Advanced Background Editing' + ].map((feature, index) => ( +
  • + + + + {feature} +
  • + ))} +
+
-
- )} - -
-
- -
- {isLoading || isModelSwitching ? ( - <> -
-

- {isModelSwitching ? 'Switching models...' : 'Loading background removal model...'} -

- - ) : error ? ( - <> - - - -

{error.message}

- {currentModel === 'Xenova/modnet' && ( - + )} + +
+
+
+ +
+ {isLoading || isModelSwitching ? ( + <> +
+

+ {isModelSwitching ? 'Switching models...' : 'Loading background removal model...'} +

+ + ) : error ? ( + <> + + + +

{error.message}

+ {currentModel === 'Xenova/modnet' && ( + + )} + + ) : ( + <> + + + +

+ {isDragActive + ? "Drop the images here..." + : "Drag and drop images here"} +

+

or click to select files

+ )} - - ) : ( - <> - - - -

- {isDragActive - ? "Drop the images here..." - : "Drag and drop images here"} -

-

or click to select files

- - )} +
+
+ +
-
- {images.length === 0 && ( -
-

Try with sample images:

-
- {sampleImages.map((url, index) => ( - - ))} + {images.length === 0 && ( +
+

Try with sample images:

+
+ {sampleImages.map((url, index) => ( + + ))} +
+

+ All images are processed locally on your device and are not uploaded to any server. +

-

- All images are processed locally on your device and are not uploaded to any server. -

-
- )} + )} - setImages(prev => prev.filter(img => img.id !== id))} /> + setImages(prev => prev.filter(img => img.id !== id))} /> +
-
+ )}