Raghvender commited on
Commit
b361607
1 Parent(s): abb346a

Save to Github

Browse files
Files changed (3) hide show
  1. .env +1 -0
  2. app.py +41 -5
  3. requirements.txt +2 -1
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ TOKEN = "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"
app.py CHANGED
@@ -2,21 +2,57 @@ import gradio as gr
2
  from ultralytics import YOLO
3
  import os
4
  from datetime import datetime
 
 
 
5
  import cv2
 
 
6
 
7
  model = YOLO('head_yolov8s.pt')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  def head_detect(path):
10
  if path is None:
11
  return None
12
 
13
  img = cv2.imread(path)
14
- # Save the original image
15
- if not os.path.exists('uploaded_images'):
16
- os.makedirs('uploaded_images')
17
  current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
18
- filename = f'uploaded_images/{current_time}.jpg'
19
- cv2.imwrite(filename, img)
 
 
 
 
 
 
20
 
21
  output = model(source=img)
22
  res = output[0].cpu().numpy()
 
2
  from ultralytics import YOLO
3
  import os
4
  from datetime import datetime
5
+ import requests
6
+ from requests.auth import HTTPBasicAuth
7
+ import base64
8
  import cv2
9
+ from dotenv import load_dotenv
10
+ import io
11
 
12
  model = YOLO('head_yolov8s.pt')
13
+ load_dotenv()
14
+
15
+ def upload_to_github(file_content, filepath):
16
+ username = "mohitsriv99"
17
+ repository = "hsamples"
18
+ branch = "main"
19
+ url = f"https://api.github.com/repos/{username}/{repository}/contents/{filepath}"
20
+ token = os.getenv("TOKEN") # Using an environment variable for the token
21
+
22
+ encoded_file = base64.b64encode(file_content).decode("utf-8")
23
+
24
+ payload = {
25
+ "message": f"Upload image {filepath}",
26
+ "content": encoded_file,
27
+ "branch": branch
28
+ }
29
+
30
+ headers = {
31
+ "Authorization": f"token {token}"
32
+ }
33
+
34
+ response = requests.put(url, json=payload, headers=headers)
35
+ if response.status_code == 201:
36
+ print("File uploaded successfully to GitHub.")
37
+ else:
38
+ print(f"Failed to upload file. Status code: {response.status_code}, Response: {response.text}")
39
 
40
  def head_detect(path):
41
  if path is None:
42
  return None
43
 
44
  img = cv2.imread(path)
45
+
46
+ # Generate a timestamped filename
 
47
  current_time = datetime.now().strftime("%Y%m%d_%H%M%S")
48
+ filename = f"uploaded_images/{current_time}.jpg"
49
+
50
+ # Encode the image to a memory buffer instead of saving to disk
51
+ _, buffer = cv2.imencode('.jpg', img)
52
+ img_byte_array = io.BytesIO(buffer).getvalue()
53
+
54
+ # Upload the image to GitHub
55
+ upload_to_github(img_byte_array, filename)
56
 
57
  output = model(source=img)
58
  res = output[0].cpu().numpy()
requirements.txt CHANGED
@@ -1,2 +1,3 @@
1
  ultralytics
2
- opencv-python
 
 
1
  ultralytics
2
+ opencv-python
3
+ python-dotenv