adnaniqbal001 commited on
Commit
6f19fcb
·
verified ·
1 Parent(s): 85feace

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -8
app.py CHANGED
@@ -1,21 +1,43 @@
1
  from flask import Flask, request, jsonify
2
  import cv2
3
  import sqlite3
 
4
 
5
  app = Flask(__name__)
6
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  @app.route('/count', methods=['POST'])
8
  def count_material():
9
- name = request.json['name']
10
- # Simulated count (replace with camera logic)
11
- count = 5
12
- conn = sqlite3.connect('materials.db')
13
- c = conn.cursor()
 
 
 
14
  c.execute("INSERT INTO counts (name, count, timestamp) VALUES (?, ?, ?)",
15
- (name, count, "2025-02-28"))
16
  conn.commit()
17
- conn.close()
18
- return jsonify({"material": name, "count": count})
 
 
 
 
 
 
19
 
20
  if __name__ == '__main__':
21
  app.run(host='0.0.0.0', port=7860)
 
1
  from flask import Flask, request, jsonify
2
  import cv2
3
  import sqlite3
4
+ from datetime import datetime
5
 
6
  app = Flask(__name__)
7
 
8
+ # Database setup
9
+ conn = sqlite3.connect('materials.db', check_same_thread=False)
10
+ c = conn.cursor()
11
+ c.execute('''CREATE TABLE IF NOT EXISTS counts
12
+ (id INTEGER PRIMARY KEY, name TEXT, count INTEGER, timestamp TEXT)''')
13
+ conn.commit()
14
+
15
+ # Dummy counting function (replace with actual camera logic)
16
+ def detect_material():
17
+ # Yeh placeholder hai; actual OpenCV camera logic yahan aayega
18
+ return 5 # Simulated count
19
+
20
  @app.route('/count', methods=['POST'])
21
  def count_material():
22
+ data = request.json
23
+ name = data.get('name', 'Unknown')
24
+
25
+ # Counting logic (camera se replace karo)
26
+ count = detect_material()
27
+ timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
28
+
29
+ # Save to database
30
  c.execute("INSERT INTO counts (name, count, timestamp) VALUES (?, ?, ?)",
31
+ (name, count, timestamp))
32
  conn.commit()
33
+
34
+ return jsonify({"material": name, "count": count, "timestamp": timestamp})
35
+
36
+ @app.route('/view', methods=['GET'])
37
+ def view_counts():
38
+ c.execute("SELECT * FROM counts")
39
+ results = c.fetchall()
40
+ return jsonify([{"id": r[0], "name": r[1], "count": r[2], "timestamp": r[3]} for r in results])
41
 
42
  if __name__ == '__main__':
43
  app.run(host='0.0.0.0', port=7860)