SuperPunk2077 commited on
Commit
bc5a08a
1 Parent(s): 12fdf90

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -73
app.py CHANGED
@@ -6,77 +6,119 @@ def greet(name):
6
  iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
  iface.launch()
8
 
9
- import os, requests, huggingface.dataset
10
- import tensorflow as tf
11
-
12
- PAGES = ['xing', 'pacific', 'gsc', 'rrc']
13
-
14
- PAGE_SIZE = 100
15
- WEIGHTS_PATH = os.path.expanduser('~/.huggingface')
16
- WEIGHTS_PATH += '/model.h5'
17
- FLATTENED_PATH = WEIGHTS_PATH + '/flattened'
18
- BELIEF_PATH = WEIGHTS_PATH + '/belief'
19
- TRAIN_DIR = os.path.expanduser('~/.huggingface')
20
- + '/models/nlp-train'
21
- DEMO_DIR = os.path.expanduser('~/.huggingface')
22
- + '/models/nlp-demo'
23
-
24
- PAGE_DIRS = []
25
- for page in PAGES:
26
- filename = page.replace('-', '_')
27
- PAGE_DIRS.append(os.path.expanduser('~/.huggingface') + '/text/{0}/pages'.format(filename))
28
-
29
- Y_TO_X = {}
30
- def add_page(page, dirs):
31
- page_dir = dirs[page]
32
- assert page_dir is not None
33
- train_page, test_page, train_test_ratio, _, _ = huggingface.dataset.read_page(page, page_dir, page_size=PAGE_SIZE)
34
- assert train_page is not None
35
- assert test_page is not None
36
- if train_test_ratio == 0.5:
37
- assert train_page.shape == (PAGE_SIZE,)
38
- assert test_page.shape == (PAGE_SIZE,)
39
- else:
40
- assert train_page.shape == (int(train_page.shape[0] * train_test_ratio),)
41
- assert test_page.shape == (PAGE_SIZE - int(train_page.shape[0] * train_test_ratio),)
42
- X = np.hstack([train_page, test_page])
43
- if page in Y_TO_X:
44
- Y = Y_TO_X[page]
45
- else:
46
- Y = list(huggingface.dataset.read_text(page).encode('utf8'))
47
- Y_TO_X[page] = Y
48
- return X, Y
49
-
50
- add_page(xing, PAGE_DIRS)
51
- add_page(pacific, PAGE_DIRS)
52
- add_page(gsc, PAGE_DIRS)
53
- add_page(rrc, PAGE_DIRS)
54
-
55
- # load model
56
- with tf.Session() as sess:
57
- model = huggingface.model.load(sess, FLATTENED_PATH, PAGE_DIRS)
58
- model.to(sess)
59
-
60
- X, Y = np.array(list(map(add_page, PAGES))), []
61
-
62
- for page in PAGES:
63
- X, Y = np.array(list(map(add_page, PAGES))), list(Y_TO_X[page])
64
-
65
- X = np.array(X)
66
- Y = np.array(Y) / 255.0
67
- X = np.reshape(X, (-1, 100, 200, 1))
68
- Y = np.reshape(Y, (-1, 10))
69
-
70
- model = tf.keras.models.Model(inputs=model.input, outputs=model.output)
71
- model.compile(optimizer=tf.keras.optimizers.Adam(),
72
- loss='categorical_crossentropy',
73
- metrics=['accuracy'])
74
- model.fit(X, Y, batch_size=100, nb_epoch=1000, verbose=2, validation_data=(X, Y))
75
- model.save_weights(WEIGHTS_PATH)
76
-
77
- BELIEF_PATH = WEIGHTS_PATH + '/belief'
78
- model.compile(optimizer=tf.keras.optimizers.Adam(),
79
- loss=tf.keras.losses.HuberLoss(delta=0.01))
80
- model.load_weights(WEIGHTS_PATH)
81
- model.fit(X, Y, batch_size=10, epochs=1)
82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
  iface.launch()
8
 
9
+ Text to image AI, python
10
+ import datetime
11
+ import time
12
+ from skimage import io, color
13
+ from PIL import Image
14
+ from io import BytesIO
15
+ from skimage.io import imread, imsave
16
+ import imutils
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
+ # Location of files on your system
19
+ PATH_TO_FILES = '/home/ekene/Documents/working/ChoiceData/imglb2017/'
20
+
21
+ # Start by importing modules
22
+ from glob import glob
23
+ import cv2
24
+ import datetime
25
+ import sys
26
+ import os
27
+ import string
28
+ import warnings
29
+ from os.path import join
30
+ import matplotlib.pyplot as plt
31
+
32
+ def main(argv):
33
+ timestamp = datetime.datetime.now().strftime("%Y%m%d-%H%M%S%f")
34
+ print("Initialising...")
35
+ if len(argv) == 2:
36
+ filename = argv[1]
37
+ full_path = PATH_TO_FILES + filename
38
+ img = imread(full_path)
39
+
40
+ scale_image = 2 # downscale the image
41
+ img = cv2.resize(img, (scale_image, scale_image))
42
+
43
+ gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
44
+ #otsu = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
45
+ #img = cv2.cvtColor(otsu, cv2.COLOR_GRAY2BGR)
46
+
47
+ # Apply histogram equalization
48
+ h, w = gray.shape[:2]
49
+ cv_he = cv2.equalizeHist(gray)
50
+ #cv2.imshow('HE', cv_he)
51
+
52
+ ualized image
53
+
54
+ _, contours, hierarchy = cv2.findContours(cv_he, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
55
+
56
+ ntourSeparation=(20,20))
57
+
58
+ # sort contours by contour area
59
+ # keep only contours that are larger than 10% of the image area
60
+ # (here we assume the largest contour will be around the "head")
61
+
62
+ filtered_contours = [c for c in contours if cv2.contourArea(c) > 1000]
63
+
64
+ #calculate distance between each contour and sort contours by distance
65
+ # print(filtered_contours, contours)
66
+ closest_contours = sorted(filtered_contours, key = cv2.contourArea, reverse=True)
67
+ closest_contours = closest_contours[:4]
68
+ # selected_contours = closest_contours
69
+
70
+ # sort by smallest contour area
71
+ #filtered_contours = sorted(filtered_contours, key = cv2.contourArea, reverse=True)
72
+
73
+ # used for testing
74
+ if len(filtered_contours) > 1:
75
+ return 0
76
+ cv2.drawContours(gray, [filtered_contours[0]], -1, (255,255,255), 2)
77
+ cv2.drawContours(gray, filtered_contours, -1, (255,255,255), 2)
78
+ cv2.imshow("search by smallest area", gray)
79
+ cv2.waitKey(0)
80
+
81
+ return 0
82
+
83
+ # adding to show a function image
84
+
85
+ def show_image(frame):
86
+ plt.subplot(1,2,1)
87
+ plt.title('Original Image')
88
+ plt.imshow(frame, cmap='gray')
89
+ plt.subplot(1,2,2)
90
+ plt.title('Image with contours drawn')
91
+ plt.imshow(frame, cmap='gray')
92
+ plt.show()
93
+ return 0
94
+
95
+ def image_face_detection(path_to_image):
96
+
97
+ # Initialise the trackbar
98
+ cv2.namedWindow('Frame')
99
+ cv2.namedWindow('Threshold')
100
+
101
+ current_image = cv2.imread(path_to_image)
102
+
103
+ frame = current_image
104
+ # threshold_trackbar = cv2.createTrackbar('Threshold', 'Frame', 51, 255, nothing)
105
+ # cv2.createTrackbar('Threshold', 'Frame', 100, 255, nothing)
106
+ # cv2.createTrackbar('Threshold', 'Frame', 130, 255, nothing)
107
+ # cv2.createTrackbar('Threshold', 'Frame', 150, 255, nothing)
108
+ # cv2.createTrackbar('Threshold', 'Frame', 90, 255, nothing)
109
+ # cv2.createTrackbar('Threshold', 'Frame', 180, 255, nothing)
110
+
111
+ #cv2.namedWindow("Frame")
112
+ #cv2.createTrackbar('Threshold', "Frame", 50, 255, nothing)
113
+
114
+ # Initialize the first frame, resize the window, start the timer
115
+ # first_time = True
116
+ first_time = True
117
+ # start_clock = time.time()
118
+ # main_loop()
119
+ # print('Run time ', time.time()-start_clock)
120
+ start_clock = time.time()
121
+
122
+ # Capture keyboard events
123
+ key = cv2.waitKey(1) & 0xFF
124
+ # Press space to pause or unpause the video