Advertisement
varun1729

Untitled

Mar 23rd, 2023
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.81 KB | None | 0 0
  1. CATS VS DOGS (WEEK5)
  2.  
  3. !mkdir -p ~/.kaggle
  4. !cp kaggle.json ~/.kaggle/
  5.  
  6. !kaggle datasets download -d salader/dogs-vs-cats
  7.  
  8. import zipfile
  9. zip_ref = zipfile.ZipFile('/content/dogs-vs-cats.zip', 'r')
  10. zip_ref.extractall('/content')
  11. zip_ref.close()
  12.  
  13. import tensorflow as tf
  14. from tensorflow import keras
  15. from keras import Sequential
  16. from keras.layers import Dense,Conv2D,MaxPooling2D,Flatten,BatchNormalization,Dropout
  17.  
  18. # generators
  19. train_ds = keras.utils.image_dataset_from_directory(
  20.     directory = '/content/train',
  21.     labels='inferred',
  22.     label_mode = 'int',
  23.     batch_size=32,
  24.     image_size=(256,256)
  25. )
  26.  
  27. validation_ds = keras.utils.image_dataset_from_directory(
  28.     directory = '/content/test',
  29.     labels='inferred',
  30.     label_mode = 'int',
  31.     batch_size=32,
  32.     image_size=(256,256)
  33. )
  34.  
  35.  
  36. # Normalize
  37. def process(image,label):
  38.     image = tf.cast(image/255. ,tf.float32)
  39.     return image,label
  40.  
  41. train_ds = train_ds.map(process)
  42. validation_ds = validation_ds.map(process)
  43.  
  44.  
  45. # create CNN model
  46.  
  47. model = Sequential()
  48.  
  49. model.add(Conv2D(32,kernel_size=(3,3),padding='valid',activation='relu',input_shape=(256,256,3)))
  50. model.add(BatchNormalization())
  51. model.add(MaxPooling2D(pool_size=(2,2),strides=2,padding='valid'))
  52.  
  53. model.add(Conv2D(64,kernel_size=(3,3),padding='valid',activation='relu'))
  54. model.add(BatchNormalization())
  55. model.add(MaxPooling2D(pool_size=(2,2),strides=2,padding='valid'))
  56.  
  57. model.add(Conv2D(128,kernel_size=(3,3),padding='valid',activation='relu'))
  58. model.add(BatchNormalization())
  59. model.add(MaxPooling2D(pool_size=(2,2),strides=2,padding='valid'))
  60.  
  61. model.add(Flatten())
  62.  
  63. model.add(Dense(128,activation='relu'))
  64. model.add(Dropout(0.1))
  65. model.add(Dense(64,activation='relu'))
  66. model.add(Dropout(0.1))
  67. model.add(Dense(1,activation='sigmoid'))
  68.  
  69.  
  70. model.summary()
  71.  
  72.  
  73. model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
  74.  
  75. history = model.fit(train_ds,epochs=10,validation_data=validation_ds)
  76.  
  77. import matplotlib.pyplot as plt
  78.  
  79. plt.plot(history.history['accuracy'],color='red',label='train')
  80. plt.plot(history.history['val_accuracy'],color='blue',label='validation')
  81. plt.legend()
  82. plt.show()
  83.  
  84. plt.plot(history.history['accuracy'],color='red',label='train')
  85. plt.plot(history.history['val_accuracy'],color='blue',label='validation')
  86. plt.legend()
  87. plt.show()
  88.  
  89. plt.plot(history.history['loss'],color='red',label='train')
  90. plt.plot(history.history['val_loss'],color='blue',label='validation')
  91. plt.legend()
  92. plt.show()
  93.  
  94. plt.plot(history.history['loss'],color='red',label='train')
  95. plt.plot(history.history['val_loss'],color='blue',label='validation')
  96. plt.legend()
  97. plt.show()
  98.  
  99. import cv2
  100. test_img = cv2.imread('/content/cat.jpg')
  101.  
  102. plt.imshow(test_img)
  103.  
  104. test_img.shape
  105.  
  106. test_img = cv2.resize(test_img,(256,256))
  107. test_input = test_img.reshape((1,256,256,3))
  108. model.predict(test_input)
  109.  
  110.  
  111.  
  112.  
  113.  
  114. WEEK-4
  115.  
  116. ADDITION/SUBTRACTION
  117.  
  118. from tkinter import *
  119. def Add():
  120.  a=int(t1.get(1.0, "end-1c"))
  121.  b=int(t2.get(1.0, "end-1c"))
  122.  rea.config(text=str(a+b))
  123. def Sub():
  124.  a=int(t1.get(1.0, "end-1c"))
  125.  b=int(t2.get(1.0, "end-1c"))
  126.  res.config(text=str(abs(a-b)))
  127. a = Tk()
  128. a.geometry("500x500")
  129. a.title("test")
  130. l1 = Label(a, text = "enter first variable")
  131. t1 = Text(a, height = 2, width = 10)
  132. l1.pack()
  133. t1.pack()
  134. l2 = Label(a, text = "enter second variable")
  135. t2 = Text(a, height = 2, width = 10)
  136. l2.pack()
  137. t2.pack()
  138. Button(a,text='Addition',command=Add).pack()
  139. l3 = Label(a, text = "Result")
  140. l3.pack()
  141. rea = Label(a, text = "")
  142. rea.pack()
  143. Button(a,text='subtraction',command=Sub).pack()
  144. l4= Label(a, text = "Result")
  145. l4.pack()
  146. res = Label(a, text = "")
  147. res.pack()
  148. a.mainloop()
  149.  
  150.  
  151. CONVERSION
  152.  
  153. rom tkinter import *
  154. def convert():
  155.  a=e1.get()
  156.  euros = float(a) / 88.46
  157.  l3.config(text=str(euros))
  158. a = Tk()
  159. a.geometry("300x200")
  160. a.title("convert")
  161. l1 = Label(a, text = "ENTER AMOUNT IN RUPEES")
  162. l1.pack()
  163. e1= Entry(a)
  164. e1.pack()
  165. l2=Label(a,text='AMOUNT IN EUROS')
  166. l2.pack()
  167. Button(a,text='convert',command=convert).pack()
  168. l3=Label(a,text='')
  169. l3.pack()
  170. a.mainloop()
  171.  
  172. STUDENT DETAILS
  173. import tkinter as tk
  174. from tkinter import *
  175. a = Tk()
  176. a.geometry("600x600")
  177. a.title("student details")
  178. l1=Label(a,text='enter your name')
  179. e1=Entry(a)
  180. l1.pack()
  181. e1.pack()
  182. l2=Label(a,text='enter your roll number')
  183. e2=Entry(a)
  184. l2.pack()
  185. e2.pack()
  186. l3=Label(a,text='enter your emailID')
  187. e3=Entry(a)
  188. l3.pack()
  189. e3.pack()
  190. g=Label(a,text='GENDER')
  191. g.pack()
  192. ge=StringVar()
  193. R1 = Radiobutton(a, text="Male", variable=ge, value='Male')
  194. R1.pack()
  195. R2 = Radiobutton(a, text="Female", variable=ge, value='Female')
  196. R2.pack()
  197. course_label = tk.Label(a, text="Course:")
  198. course_label.pack()
  199. course_listbox = tk.Listbox(a)
  200. course_listbox.insert(1, "B.Tech")
  201. course_listbox.insert(2, "M.Tech")
  202. course_listbox.insert(3, "PhD")
  203. course_listbox.pack()
  204. br=Label(a,text='CHOOSE BRANCH')
  205. br.pack()
  206. b=Listbox(a)
  207. b.insert(1,'CSE')
  208. b.insert(2,'CIVIL')
  209. b.insert(3,'IT')
  210. b.insert(4,'ECE')
  211. b.insert(5,'EEE')
  212. b.pack()
  213. Button(a,text='SUBMIT').pack()
  214. a.mainloop()
  215.  
  216.  
  217. WEEK-2
  218.  
  219. FROM IMAGE
  220.  
  221. import numpy as np
  222. import cv2
  223. csc= r"haarcascade_eye.xml"
  224. csc2= r"haarcascade_frontalface_default.xml"
  225. face_cascade=cv2.CascadeClassifier(csc2)
  226. eye_cascade=cv2.CascadeClassifier(csc)
  227. img=cv2.imread("C:\WEEK2DL\img_1.png")
  228. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  229. faces = face_cascade.detectMultiScale(gray, 1.3, 10)
  230. for(x,y,w,h) in faces:
  231.     cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
  232.     roi_gray=gray[y:y+h,x:x+w]
  233.     roi_color=img[y:y+h,x:x+w]
  234.     eyes = eye_cascade.detectMultiScale(roi_gray)
  235. for (ex,ey,ew,eh) in eyes:
  236.     cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
  237.     cv2.imshow('img',img)
  238.     cv2.waitKey(0)
  239. cv2.destroyAllWindows()
  240.  
  241. from cam
  242.  
  243. import cv2
  244.  
  245. cap = cv2.VideoCapture(0)  # 0 for default camera, change if necessary
  246.  
  247. while True:
  248.     ret, frame = cap.read()  # read a frame from the camera
  249.  
  250.     if ret:  # if frame is successfully read
  251.         gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # convert to grayscale
  252.  
  253.         # apply face detection
  254.         face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
  255.         eye_cascade=cv2.CascadeClassifier('haarcascade_eye.xml')
  256.         faces = face_cascade.detectMultiScale(gray, 1.3, 5)
  257.         eyes = eye_cascade.detectMultiScale(gray, 1.3, 5)
  258.  
  259.         # draw rectangles around detected faces
  260.         for (x, y, w, h) in faces:
  261.             cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
  262.         for (x, y, w, h) in eyes:
  263.             cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
  264.  
  265.         # show the captured frame
  266.         cv2.imshow('frame', frame)
  267.        
  268.     # exit the loop if 'q' key is pressed
  269.     if cv2.waitKey(1) & 0xFF == ord('q'):
  270.         break
  271.  
  272. cap.release()  # release the camera
  273. cv2.destroyAllWindows()  # close all windows
  274.  
  275.  
  276.  
  277.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement