Hashii1729 commited on
Commit
d7e6224
·
1 Parent(s): 2805777

Refactor analysis endpoints: remove deprecated response models, enhance JSON output formatting, and improve object detection functionality

Browse files
Files changed (1) hide show
  1. fast.py +59 -54
fast.py CHANGED
@@ -1741,9 +1741,6 @@ class StructuredAnalysisResponse(BaseModel):
1741
  confidence_score: float
1742
  detected_items: List[Dict[str, Any]] = []
1743
 
1744
- class AnalysisResponse(BaseModel):
1745
- analysis: str
1746
-
1747
  class DetailedAnalysisResponse(BaseModel):
1748
  structured_analysis: StructuredAnalysisResponse
1749
  raw_analysis: str
@@ -1766,15 +1763,15 @@ async def root():
1766
  </style>
1767
  </head>
1768
  <body>
1769
- <h1>🎽 Fashion Analyzer</h1>
1770
- <p>Upload an image of clothing to get detailed fashion analysis</p>
1771
 
1772
  <div class="upload-area">
1773
  <input type="file" id="imageInput" accept="image/*" style="margin: 10px;">
1774
  <br>
1775
- <button onclick="analyzeImage()" style="padding: 10px 20px; margin: 10px;">Analyze Fashion (Detailed)</button>
1776
- <button onclick="analyzeStructured()" style="padding: 10px 20px; margin: 10px;">Analyze Fashion (Structured)</button>
1777
- <button onclick="analyzeJSON()" style="padding: 10px 20px; margin: 10px; background-color: #28a745; color: white;">Analyze Fashion (JSON)</button>
1778
  <button onclick="checkDeepFashion2Status()" style="padding: 10px 20px; margin: 10px; background-color: #6f42c1; color: white;">DeepFashion2 Status</button>
1779
  <br>
1780
  <a href="/refined-prompt" target="_blank" style="color: #007bff; text-decoration: none;">View Refined Prompt Format</a>
@@ -1798,7 +1795,7 @@ async def root():
1798
  const formData = new FormData();
1799
  formData.append('file', file);
1800
 
1801
- document.getElementById('analysisText').textContent = 'Analyzing... Please wait...';
1802
  document.getElementById('result').style.display = 'block';
1803
 
1804
  try {
@@ -1808,7 +1805,20 @@ async def root():
1808
  });
1809
 
1810
  const result = await response.json();
1811
- document.getElementById('analysisText').textContent = result.analysis;
 
 
 
 
 
 
 
 
 
 
 
 
 
1812
  } catch (error) {
1813
  document.getElementById('analysisText').textContent = 'Error: ' + error.message;
1814
  }
@@ -1826,7 +1836,7 @@ async def root():
1826
  const formData = new FormData();
1827
  formData.append('file', file);
1828
 
1829
- document.getElementById('analysisText').textContent = 'Analyzing with structured format... Please wait...';
1830
  document.getElementById('result').style.display = 'block';
1831
 
1832
  try {
@@ -1836,13 +1846,18 @@ async def root():
1836
  });
1837
 
1838
  const result = await response.json();
1839
- document.getElementById('analysisText').textContent = result.analysis;
 
 
 
 
 
1840
  } catch (error) {
1841
  document.getElementById('analysisText').textContent = 'Error: ' + error.message;
1842
  }
1843
  }
1844
 
1845
- async function analyzeJSON() {
1846
  const input = document.getElementById('imageInput');
1847
  const file = input.files[0];
1848
 
@@ -1854,11 +1869,11 @@ async def root():
1854
  const formData = new FormData();
1855
  formData.append('file', file);
1856
 
1857
- document.getElementById('analysisText').textContent = 'Analyzing with JSON format... Please wait...';
1858
  document.getElementById('result').style.display = 'block';
1859
 
1860
  try {
1861
- const response = await fetch('/analyze-json', {
1862
  method: 'POST',
1863
  body: formData
1864
  });
@@ -1866,14 +1881,8 @@ async def root():
1866
  const result = await response.json();
1867
 
1868
  // Format JSON output nicely
1869
- let jsonOutput = 'JSON FASHION ANALYSIS RESULT:\\n\\n';
1870
- jsonOutput += 'STRUCTURED ANALYSIS:\\n';
1871
- jsonOutput += JSON.stringify(result.structured_analysis, null, 2);
1872
- jsonOutput += '\\n\\nPROCESSING INFO:\\n';
1873
- jsonOutput += `Processing Time: ${result.processing_time.toFixed(3)} seconds\\n`;
1874
- jsonOutput += `Device: ${result.model_info.device}\\n`;
1875
- jsonOutput += `Detection Model: ${result.model_info.detection_model}\\n`;
1876
- jsonOutput += `Feature Model: ${result.model_info.feature_model}\\n`;
1877
 
1878
  document.getElementById('analysisText').textContent = jsonOutput;
1879
  } catch (error) {
@@ -1956,38 +1965,8 @@ async def root():
1956
  </html>
1957
  """
1958
 
1959
- @app.post("/analyze-image", response_model=AnalysisResponse)
1960
  async def analyze_image(file: UploadFile = File(...)):
1961
- """Analyze uploaded image"""
1962
- try:
1963
- # Read image bytes
1964
- image_bytes = await file.read()
1965
-
1966
- # Analyze the clothing
1967
- analysis = analyzer.analyze_clothing_from_bytes(image_bytes)
1968
-
1969
- return AnalysisResponse(analysis=analysis)
1970
-
1971
- except Exception as e:
1972
- raise HTTPException(status_code=500, detail=f"Error analyzing image: {str(e)}")
1973
-
1974
- @app.post("/analyze-structured", response_model=AnalysisResponse)
1975
- async def analyze_structured(file: UploadFile = File(...)):
1976
- """Analyze uploaded image with structured format"""
1977
- try:
1978
- # Read image bytes
1979
- image_bytes = await file.read()
1980
-
1981
- # Get structured analysis
1982
- analysis = analyzer.analyze_clothing_structured_format(image_bytes)
1983
-
1984
- return AnalysisResponse(analysis=analysis)
1985
-
1986
- except Exception as e:
1987
- raise HTTPException(status_code=500, detail=f"Error analyzing image: {str(e)}")
1988
-
1989
- @app.post("/analyze-json", response_model=DetailedAnalysisResponse)
1990
- async def analyze_json(file: UploadFile = File(...)):
1991
  """Analyze uploaded image and return structured JSON response"""
1992
  try:
1993
  start_time = time.time()
@@ -2026,6 +2005,32 @@ async def analyze_json(file: UploadFile = File(...)):
2026
  except Exception as e:
2027
  raise HTTPException(status_code=500, detail=f"Error analyzing image: {str(e)}")
2028
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2029
  @app.post("/detect-objects", response_model=Dict[str, Any])
2030
  async def detect_objects(file: UploadFile = File(...)):
2031
  """Detect fashion objects and return JSON structure"""
 
1741
  confidence_score: float
1742
  detected_items: List[Dict[str, Any]] = []
1743
 
 
 
 
1744
  class DetailedAnalysisResponse(BaseModel):
1745
  structured_analysis: StructuredAnalysisResponse
1746
  raw_analysis: str
 
1763
  </style>
1764
  </head>
1765
  <body>
1766
+ <h1>🎽 Fashion Analyzer - JSON API</h1>
1767
+ <p>Upload an image of clothing to get detailed fashion analysis in structured JSON format</p>
1768
 
1769
  <div class="upload-area">
1770
  <input type="file" id="imageInput" accept="image/*" style="margin: 10px;">
1771
  <br>
1772
+ <button onclick="analyzeImage()" style="padding: 10px 20px; margin: 10px; background-color: #28a745; color: white;">Analyze Fashion (Detailed JSON)</button>
1773
+ <button onclick="analyzeStructured()" style="padding: 10px 20px; margin: 10px; background-color: #007bff; color: white;">Analyze Fashion (Structured JSON)</button>
1774
+ <button onclick="detectObjects()" style="padding: 10px 20px; margin: 10px; background-color: #fd7e14; color: white;">Detect Objects Only</button>
1775
  <button onclick="checkDeepFashion2Status()" style="padding: 10px 20px; margin: 10px; background-color: #6f42c1; color: white;">DeepFashion2 Status</button>
1776
  <br>
1777
  <a href="/refined-prompt" target="_blank" style="color: #007bff; text-decoration: none;">View Refined Prompt Format</a>
 
1795
  const formData = new FormData();
1796
  formData.append('file', file);
1797
 
1798
+ document.getElementById('analysisText').textContent = 'Analyzing with detailed JSON format... Please wait...';
1799
  document.getElementById('result').style.display = 'block';
1800
 
1801
  try {
 
1805
  });
1806
 
1807
  const result = await response.json();
1808
+
1809
+ // Format JSON output nicely
1810
+ let jsonOutput = 'DETAILED FASHION ANALYSIS RESULT:\\n\\n';
1811
+ jsonOutput += 'STRUCTURED ANALYSIS:\\n';
1812
+ jsonOutput += JSON.stringify(result.structured_analysis, null, 2);
1813
+ jsonOutput += '\\n\\nRAW ANALYSIS:\\n';
1814
+ jsonOutput += result.raw_analysis;
1815
+ jsonOutput += '\\n\\nPROCESSING INFO:\\n';
1816
+ jsonOutput += `Processing Time: ${result.processing_time.toFixed(3)} seconds\\n`;
1817
+ jsonOutput += `Device: ${result.model_info.device}\\n`;
1818
+ jsonOutput += `Detection Model: ${result.model_info.detection_model}\\n`;
1819
+ jsonOutput += `Feature Model: ${result.model_info.feature_model}\\n`;
1820
+
1821
+ document.getElementById('analysisText').textContent = jsonOutput;
1822
  } catch (error) {
1823
  document.getElementById('analysisText').textContent = 'Error: ' + error.message;
1824
  }
 
1836
  const formData = new FormData();
1837
  formData.append('file', file);
1838
 
1839
+ document.getElementById('analysisText').textContent = 'Analyzing with structured JSON format... Please wait...';
1840
  document.getElementById('result').style.display = 'block';
1841
 
1842
  try {
 
1846
  });
1847
 
1848
  const result = await response.json();
1849
+
1850
+ // Format JSON output nicely
1851
+ let jsonOutput = 'STRUCTURED FASHION ANALYSIS RESULT:\\n\\n';
1852
+ jsonOutput += JSON.stringify(result, null, 2);
1853
+
1854
+ document.getElementById('analysisText').textContent = jsonOutput;
1855
  } catch (error) {
1856
  document.getElementById('analysisText').textContent = 'Error: ' + error.message;
1857
  }
1858
  }
1859
 
1860
+ async function detectObjects() {
1861
  const input = document.getElementById('imageInput');
1862
  const file = input.files[0];
1863
 
 
1869
  const formData = new FormData();
1870
  formData.append('file', file);
1871
 
1872
+ document.getElementById('analysisText').textContent = 'Detecting fashion objects... Please wait...';
1873
  document.getElementById('result').style.display = 'block';
1874
 
1875
  try {
1876
+ const response = await fetch('/detect-objects', {
1877
  method: 'POST',
1878
  body: formData
1879
  });
 
1881
  const result = await response.json();
1882
 
1883
  // Format JSON output nicely
1884
+ let jsonOutput = 'FASHION OBJECT DETECTION RESULT:\\n\\n';
1885
+ jsonOutput += JSON.stringify(result, null, 2);
 
 
 
 
 
 
1886
 
1887
  document.getElementById('analysisText').textContent = jsonOutput;
1888
  } catch (error) {
 
1965
  </html>
1966
  """
1967
 
1968
+ @app.post("/analyze-image", response_model=DetailedAnalysisResponse)
1969
  async def analyze_image(file: UploadFile = File(...)):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1970
  """Analyze uploaded image and return structured JSON response"""
1971
  try:
1972
  start_time = time.time()
 
2005
  except Exception as e:
2006
  raise HTTPException(status_code=500, detail=f"Error analyzing image: {str(e)}")
2007
 
2008
+ @app.post("/analyze-structured", response_model=StructuredAnalysisResponse)
2009
+ async def analyze_structured(file: UploadFile = File(...)):
2010
+ """Analyze uploaded image with structured format and return JSON"""
2011
+ try:
2012
+ # Read image bytes
2013
+ image_bytes = await file.read()
2014
+
2015
+ # Process image
2016
+ image = analyzer.process_image_from_bytes(image_bytes)
2017
+
2018
+ # Get fashion object detection results
2019
+ detection_results = analyzer.detect_fashion_objects(image)
2020
+
2021
+ # Get basic image description
2022
+ basic_description = analyzer.get_basic_description(image)
2023
+
2024
+ # Extract structured information
2025
+ structured_analysis = analyzer.parse_structured_analysis(detection_results, basic_description)
2026
+
2027
+ return structured_analysis
2028
+
2029
+ except Exception as e:
2030
+ raise HTTPException(status_code=500, detail=f"Error analyzing image: {str(e)}")
2031
+
2032
+
2033
+
2034
  @app.post("/detect-objects", response_model=Dict[str, Any])
2035
  async def detect_objects(file: UploadFile = File(...)):
2036
  """Detect fashion objects and return JSON structure"""