karthikmohan409 commited on
Commit
dac50dc
1 Parent(s): b1492c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -68
app.py CHANGED
@@ -1,71 +1,39 @@
1
- from flask import Flask, request, jsonify, send_from_directory
2
- from flask_cors import CORS
3
- from genai import gen_vton
4
- from werkzeug.utils import secure_filename
5
  import os
6
  import tempfile
 
 
7
 
8
- #app = Flask(__name__)
9
-
10
- app = Flask(__name__, static_folder='processed_images')
11
-
12
- CORS(app, supports_credentials=True)
13
- #CORS(app, supports_credentials=True, resources={r"/*": {"origins": "*"}}) # Allow requests from any originorigins=["http://localhost:3000"])
14
-
15
- #CORS(app, resources={r"/proc": {"origins": "http://localhost:3000"}}, supports_credentials=True)
16
- #@app.route("/proc")
17
- @app.route('/proc', methods=['POST'])
18
- def process_images():
19
- # Retrieve images from the request
20
- print("Request came here")
21
- print(request)
22
- print(request.headers)
23
- print(request.files)
24
-
25
-
26
- user_image_t = request.files.get('userImage')
27
- dress_image_t = request.files.get('dressImage')
28
- #print(dress_image_t.filename)
29
- print(user_image_t.filename)
30
- #file = request.files['file']
31
- if dress_image_t:
32
- # Save the file to a temporary file
33
- temp_dir = tempfile.gettempdir()
34
- filename = secure_filename(dress_image_t.filename)
35
- temp_path = os.path.join(temp_dir, filename)
36
- dress_image_t.save(temp_path)
37
- dress_image = temp_path
38
- if user_image_t:
39
- temp_dir = tempfile.gettempdir()
40
- filename = secure_filename(user_image_t.filename)
41
- temp_path_1 = os.path.join(temp_dir, filename)
42
- user_image_t.save(temp_path_1)
43
- user_image = temp_path_1
44
-
45
- gen_vton(user_image, dress_image)
46
- processed_image_1_path = './processed_images/output_image.jpg'
47
- processed_image_2_path = './processed_images/output_image_1.jpg'
48
-
49
- # Save your images using the paths above...
50
-
51
- # Return the URL for the saved images
52
- url_to_processed_image_1 = request.host_url + processed_image_1_path
53
- url_to_processed_image_2 = request.host_url + processed_image_2_path
54
- # Process images...
55
- # For the sake of this example, let's say the processing function returns two image URLs
56
- processed_image_urls = [url_to_processed_image_1, url_to_processed_image_2]
57
- os.remove(temp_path)
58
- os.remove(temp_path_1)
59
- return jsonify({'processedImages': processed_image_urls})
60
-
61
- @app.route('/processed_images/<filename>')
62
- def processed_images(filename):
63
- print("request_came_here")
64
- return send_from_directory(app.static_folder, filename)
65
- # Example of generating a unique filename for the output
66
-
67
-
68
- #
69
-
70
- if __name__ == '__main__':
71
- app.run()
 
1
+ import streamlit as st
2
+ from PIL import Image
 
 
3
  import os
4
  import tempfile
5
+ from genai import gen_vton
6
+ from werkzeug.utils import secure_filename
7
 
8
+ # Define a function to save the uploaded file to a temporary directory
9
+ def save_uploaded_file(uploaded_file):
10
+ try:
11
+ with tempfile.NamedTemporaryFile(delete=False, suffix="." + uploaded_file.name.split('.')[1]) as tmp:
12
+ tmp.write(uploaded_file.getbuffer())
13
+ return tmp.name
14
+ except Exception as e:
15
+ return None
16
+
17
+ # Streamlit UI components
18
+ st.title('Image Processing with gen_vton')
19
+
20
+ # File uploader for the user and dress images
21
+ user_image_up = st.file_uploader("Upload User Image", type=['jpg', 'png', 'jpeg'])
22
+ dress_image_up = st.file_uploader("Upload Dress Image", type=['jpg', 'png', 'jpeg'])
23
+
24
+ # Process the images once both are uploaded and a button is pressed
25
+ if user_image_up and dress_image_up and st.button('Process Images'):
26
+ user_image_path = save_uploaded_file(user_image_up)
27
+ dress_image_path = save_uploaded_file(dress_image_up)
28
+
29
+ # Assuming gen_vton processes the images and saves them somewhere
30
+ processed_image_paths = gen_vton(user_image_path, dress_image_path) # Adjust based on actual return value
31
+
32
+ # Displaying processed images (assuming gen_vton returns paths to processed images)
33
+ if processed_image_paths:
34
+ for image_path in processed_image_paths:
35
+ st.image(image_path, use_column_width=True)
36
+
37
+ # Clean up temporary files
38
+ os.remove(user_image_path)
39
+ os.remove(dress_image_path)