Browse Source

migration

jezv 1 year ago
commit
d202228b2d
6 changed files with 220 additions and 0 deletions
  1. 31 0
      figures/main.py
  2. BIN
      figures/ps.npy.txt
  3. 23 0
      gradient/main.py
  4. 75 0
      numpy_task/main.py
  5. 41 0
      remote_start/main.py
  6. 50 0
      wires/main.py

+ 31 - 0
figures/main.py

@@ -0,0 +1,31 @@
+import numpy as np
+import matplotlib.pyplot as plt
+from skimage.measure import label
+from pathlib import Path
+
+img = np.load(Path(Path.cwd(), 'figures', 'ps.npy.txt'))
+
+labeled = label(img)
+
+figures = {}
+
+# Частотный словарь
+
+for lbl in np.unique(labeled)[1:]:
+    x, y = np.where(labeled == lbl)
+    x = x - x.min()
+    y = y - y.min()
+    key = (str(x), str(y))
+    if key not in figures.keys():
+        figures[key] = 1
+    else: figures[key] += 1
+
+for i, (figure, coun) in enumerate(figures.items()):
+    plt.subplot(1,5,i+1)
+    figure = (np.fromstring(figure[0][1:-1], dtype=int, sep=' '), np.fromstring(figure[1][1:-1], dtype=int, sep=' '))
+    fig = np.zeros((max(figure[0])+1,max(figure[1])+1))
+    fig[figure[0], figure[1]] = 1
+    plt.title(f"{coun}")
+    plt.imshow(fig)
+plt.suptitle(f"Всего: {labeled.max()}")
+plt.show()

BIN
figures/ps.npy.txt


+ 23 - 0
gradient/main.py

@@ -0,0 +1,23 @@
+import numpy as np
+import matplotlib.pyplot as plt
+
+
+color1 = [255, 128, 0]
+color2 = [0, 128, 255]
+
+
+def create_gradient(color1, color2, size_x, size_y):
+    layers =  []
+    for i in range(3):
+        x = np.linspace(color2[i]/2, color1[i]/2, size_x)
+        y = np.linspace(color2[i]/2, color1[i]/2, size_y)
+        x = x.reshape((100, 1))
+        layer = (x+y) / 255
+        print(layer)
+        layers.append(layer)
+    return np.dstack(layers)
+
+
+img = create_gradient(color1, color2, 100, 100)
+plt.imshow(img)
+plt.show()

+ 75 - 0
numpy_task/main.py

@@ -0,0 +1,75 @@
+import numpy as np
+import unittest
+
+a = np.array([3, 2, 1], dtype='uint8')
+
+assert a.dtype == "uint8"
+
+b = np.zeros((5,5))
+
+assert b.shape == (5, 5) and b.sum() == 0
+
+c = np.ones((5,5,5))
+
+assert c.ndim == 3 and c.sum() / c.size == 1
+
+d = np.arange(-5, 5, 1)
+
+assert np.all(d == np.array([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4]))
+
+e = np.linspace(0, 1, 5)
+
+assert np.all(e == np.array([0., 0.25, 0.5, 0.75, 1.0]))
+
+f = np.arange(5 * 5).reshape(5, 5)
+fc = f[::2,[1,3]]
+
+assert np.all(fc == np.array([[1, 3], [11, 13], [21, 23]]))
+
+g = np.ones((5, 3))
+gc = np.sum(g, axis=1)
+
+assert np.all(gc == np.array([3., 3., 3., 3., 3.]))
+
+h = np.arange(5) + 1
+hc = h*2
+
+assert np.all(hc == np.array([2., 4., 6., 8., 10.]))
+
+j = np.array([1, 2, 3, 4, 9, 7, 11, 12, 15, 14, 33])
+mask = (j % 3 == 0)
+jc = j[mask]
+
+assert np.all(jc == np.array([3, 9, 12, 15, 33]))
+
+k = np.array([1, 2, 3, 4, 5])
+l = np.array([2, 2, 3, 3, 4])
+kl = k**l
+
+assert np.all(kl == np.array([1, 4, 27, 64, 625]))
+
+m = np.array([2, 2, 2, 3, 3, 3])
+mc = m.std()
+
+assert mc == 0.5
+
+n = np.array([1, 2, 3, 4, 5, 6])
+nc = n.mean()
+
+assert nc == 3.5
+
+o = np.array([2, 2, 2, 2])
+oc = o.reshape((2,2))
+
+assert oc.ndim == 2 and oc.shape == (2, 2)
+
+p = np.array([1, 2, 3, 4])
+pc = p[::-1]
+
+assert np.all(pc == np.array([4, 3, 2, 1]))
+
+r = np.array([3, 3, 5, 5])
+rc = r.copy()
+rc[1:3] = -1
+
+assert np.all(r[1:3] == np.array([3, 5])) and np.all(rc[1:3] == np.array([-1, -1]))

+ 41 - 0
remote_start/main.py

@@ -0,0 +1,41 @@
+import numpy as np
+import socket
+
+host = "84.237.21.36"
+port = 5152
+
+
+def recvall(sock, n):
+    data = bytearray()
+    while len(data) < n:
+        packet = sock.recv(n - len(data))
+        if not packet:
+            return
+        data.extend(packet)
+    return data
+
+
+with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
+    sock.connect((host, port))
+
+    for _ in range(10):
+        sock.send(b"get")
+        bits = recvall(sock, 40002)
+        print(len(bits))
+
+        img = np.frombuffer(bits[2:], dtype="uint8").reshape(bits[0], bits[1])
+
+        centers = []
+        for y in range(1,img.shape[0]-2):
+            for x in range(1,img.shape[1]-2):
+                if img[y, x] > img[y, x-1] and img[y, x] > img[y, x+1] and img[y, x] > img[y+1, x] and img[y, x] > img[y-1, x]:
+                    centers.append(np.array([y,x]))
+
+    
+        res = ((centers[0][0] - centers[1][0]) ** 2 + (centers[0][1] - centers[1][1]) ** 2) ** 0.5
+        sock.send(f"{res:.1f}".encode())
+        ye = sock.recv(20)
+        print(ye)
+
+    sock.send(b'beat')
+    print("done:",sock.recv(20))

+ 50 - 0
wires/main.py

@@ -0,0 +1,50 @@
+from skimage.measure import label
+import matplotlib.pyplot as plt
+import numpy as np
+
+struct = np.ones((3,3))
+
+def dilation(arr, struct=struct):
+  result = np.zeros_like(arr)
+  for y in range(1, arr.shape[0]-1):
+    for x in range(1, arr.shape[1]-1):
+      sub = arr[y-1:y+2, x-1:x+2]
+      rsub = np.logical_and(arr[y,x], struct)
+      result[y-1:y+2, x-1:x+2] = np.logical_or(result[y-1:y+2, x-1:x+2], rsub)
+  return result
+
+def errosion(arr, struct=struct):
+  result = np.zeros_like(arr)
+  for y in range(1, arr.shape[0]-1):
+    for x in range(1, arr.shape[1]-1):
+      sub = arr[y-1:y+2, x-1:x+2]
+      if np.all(sub >= struct):
+        result[y, x] = 1
+  return result
+
+def closing(arr, struct=struct):
+  return dilation(errosion(arr, struct), struct)
+
+def opening(arr, struct=struct):
+  return errosion(dilation(arr, struct), struct)
+
+img = np.load('wires5.npy.txt')
+
+struct = np.array([[0,1,0],
+                   [0,1,0],
+                   [0,1,0]])
+
+plt.subplot(1,2,1)
+plt.imshow(img)
+plt.subplot(1,2,2)
+plt.imshow(closing(img, struct))
+
+for wire in np.unique(label(img))[1:]:
+  # print(np.where(label(img)==wire))
+  res = label(closing(img, struct))[label(img)==wire]
+  match(len(np.unique(res)[1:])):
+    case 1: print("Провод не порван")
+    case 0: print("Провод уничтожен")
+    case _: print(f"Провод {wire} порван на {len(np.unique(res)[1:])}")
+
+plt.show()