A sewing machine including a needle bar that has a sewing needle attached in a lower end thereof; a needle-bar vertical drive mechanism that vertically moves the needle bar by a sewing machine motor via a sewing machine motor main shaft; an imaging unit that captures image of at least the sewing needle and a moving object that is in close proximity of the sewing needle; a distance calculating portion that calculates a distance between the sewing needle and the moving object based on image data captured by the imaging unit; a judging portion that makes a judgment that the distance calculated by the distance calculating portion is equal to or less than a predetermined distance; and a control portion that controls execution of a predetermined safety operation based on a judgment result of the judging portion.