from collections import namedtuple
Point = namedtuple('Point',['x','y'])
p =
p
p.x, p.y
Employee = namedtuple('Employee',['name','age','department'])
employee = Employee("John",100,'Backend')
employee
employee._asdict()
Book = namedtuple('Book',['title','author','genre'])
books = [
Book('A','B','C'),
Book('A','B','C'),
Book('A','B','C')
]
from collections import deque
d = deque(['task1','task2','task3'])
d.append('task4')
d
d.pop()
d
d.popleft()
d
%%time
d = deque([i for i in range(1000000)])
for _ in range(10000):
d.pop()
%%time
l = [ i for i in range(1000000) ]
for _ in range(10000):
l.pop()
!ls
!lscpu
from multiprocessing import Process
def process_function():
print("프로세스 실행 중")
process = Process(target=process_function)
process.start()
process.join()
def work(id):
print(f"Process {id} working...")
processes = [Process(target=work, args = (i,))
for i in range(2)]
for p in processes:
p.start()
for p in processes:
p.join()
from multiprocessing import Process, Value, Array
def add_one(number, array):
number.value += 1
for i in range(len(array)):
array[i] += 1
number = Value('i',0)
array = Array('i',range(5))
processes = [Process(target=add_one,args=(number,array)) for _ in range(2)]
for p in processes:
p.start()
for p in processes:
p.join()
print(f"number : {number.value}")
print(f"array : {list(array)}")
from multiprocessing import Pool
def square(n):
return n**2
with Pool(2) as p:
results = p.map(square,range(10))
results
import pickle
data = {'a' : [i for i in range(10)],
'b' : list("hello"),
'c' : {True, False}
}
data, type(data)
with open('data.pickle','wb') as f:
pickle.dump(data,f)
with open('data.pickle','rb') as f:
data1 = pickle.load(f)
data1
class A:
name = 'good'
a = A()
with open('a.pickle','wb') as f:
pickle.dump(a,f)
with open('a.pickle','rb') as f:
aa = pickle.load(f)
aa.name
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
print("Array Addition:", arr1 + arr2)
print("Array Multiplication:", arr1 * arr2)
arr = np.array([1, 2, 3, 4, 5])
print("Mean:", np.mean(arr))
print("Standard Deviation:", np.std(arr))
arr = np.array([1,2,3])
arr
arr + 1
arr = np.array([[1,2,3],[4,5,6]])
arr
arr + 1
arr + np.array([11,12])
arr + np.array([11,12,13])
a = np.array([[1,2],[3,4]])
b = np.array([[5,6],[7,8]])
a + b
np.int8
2**8
np.float32
from PIL import Image
image = Image.open("backend.png")
image
image.save("output.png")
rotated_image = image.rotate(180)
rotated_image.save("rotated_image.png")
bw_image = image.convert("L")
bw_image.save("gray.png")
cropped_image = image.crop((100,100,400,400))
cropped_image.save("cropped.png")
from PIL import Image, ImageDraw, ImageFont
image_new = Image.open("backend.png")
draw = ImageDraw.Draw(image_new)
font = ImageFont.load_default()
draw.text((10,10),"Python Backend",fill="black",font=font)
image_new.save("text_added.png")
from PIL import ImageFilter
image = Image.open("backend.png")
blurred_image = image.filter(ImageFilter.GaussianBlur(3))
blurred_image.save("blurred.png")
import os
input_folder = "input_images"
output_folder = "output_images"
for filename in os.listdir(input_folder):
img_path = os.path.join(input_folder,filename)
img = Image.open(img_path)
resized_img = img.resize((128,128))
resized_img.save(os.path.join(output_folder, filename))
image = Image.open("input_images/backend.png")
from PIL import ImageEnhance
enhancer = ImageEnhance.Contrast(image)
enhanced_image = enhancer.enhance(2)
enhanced_image.save("enhanced_contrast.png")
np.array(image)
np.array(image).shape
array = np.random.randint(0,255,size=(440,680,3),dtype=np.uint8)
array.dtype
import cv2
import mediapipe as mp
mp_pose = mp.solutions.pose
pose = mp_pose.Pose(static_image_mode=False, model_complexity=1, smooth_landmarks=True, min_detection_confidence=0.5, min_tracking_confidence=0.5)
mp_drawing = mp.solutions.drawing_utils
cap = cv2.VideoCapture('video.mp4')
img_bool = True
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = pose.process(frame_rgb)
if results.pose_landmarks:
mp_drawing.draw_landmarks(frame, results.pose_landmarks, mp_pose.POSE_CONNECTIONS,
mp_drawing.DrawingSpec(color=(245,117,66), thickness=2, circle_radius=2),
mp_drawing.DrawingSpec(color=(245,66,230), thickness=2, circle_radius=2))
if cv2.waitKey(1) == ord('q'):
break
cv2.imshow('frame', frame)
cap.release()
cv2.destroyAllWindows()